su和su -的区别 形成shell变量的不一样

su命令是用来切换用户身份的,默认状况下敲su并回车将切换到root身份,固然须要输入密码才能切换。linux

su [用户名]将会切换到指定用户,例如将用户从leconte切换到linuxershell

[leconte@localhost ~]$ id uid=500(leconte) gid=500(leconte) groups=500(leconte) [leconte@localhost ~]$ [leconte@localhost ~]$ su linuxer 口令: bash-3.2$ id uid=501(linuxer) gid=501(linuxer) groups=501(linuxer) bash-3.2$这个用法想必你们平时都用过,也很清楚。bash

可是须要注意的是su的另一个参数“-”,当指定该参数的时候,将会进入一个“login shell” ,即和该用户登陆的状况彻底同样。而不带参数“-”的时候进入的是一个“non-login shell”。那么问题就归结到“login shell”和“non-login shell“的区别上来了。ui

它们的差异在于,对于一个登陆shell,bash在进入的时候会执行/etc/profile,~/.bash_profile,~/.bash_login, and ~/.profile中的内容,退出的时候会执行~/.bash_logout中的内容。而对于一个非登陆shell,bash进入的时候会执行/etc/bash.bashrc,~/.bashrc中的内容。.net

经过一个例子来看一下,假如用户linuxer的 .bash_profile和.bashrc内容分别以下:get

bash-3.2$ cat .bash_profile登录

.bash_profile

export TEST=login-shell bash-3.2$ cat .bashrc变量

.bashrc

export TEST2=non-login-shell很简单,咱们在文件里分别设置了两个环境变量,而后用su linuxer和su -linuxer分别进入后查看环境变量,结果以下file

[leconte@localhost ~]$ su linuxer 口令: bash-3.2$ env | grep TEST TEST2=non-login-shell[leconte@localhost ~]$ su - linuxer 口令: -bash-3.2$ env | grep TEST TEST=login-shell能够看到,su linuxer进入非登陆shell,.bashrc被执行;su – linuxer进入登陆shell,.bash_profile被执行。grep

相关文章
相关标签/搜索