su使用时环境变量注意事项

背景:在测试机中,程序配置rc.local自启动 结果总是乱码的。后面经测试才知道要添加 -l 参数

-l 或 –login 这个参数加了之后,就好像是重新 login 为该使用者一样,大部份环境变数(HOME SHELL USER等等)都是以该使用者(USER)为主,并且工作目录也会改变

没有添加-l 参数

[root@eant ~]#su - wslogic  -c  'env'   
DISPLAY=localhost:10.0
USER=wslogic
PWD=/home/wslogic
HOME=/home/wslogic
SHELL=/bin/bash
TERM=linux
SHLVL=1
LOGNAME=wslogic
PATH=/usr/local/bin:/bin:/usr/bin
_=/bin/env
[root@eant ~]#

添加-l 参数

[root@rant ~]#su - wslogic  -c  -l 'env'
WL_HOME=/home/wslogic/Middleware/wlserver_10.3
LANG=zh_CN.UTF-8
HISTCONTROL=ignoredups
DISPLAY=localhost:10.0
HOSTNAME=eant
JAVA_HOME=/home/wslogic/jrockit-jdk1.6.0_45-R28.2.7-4.1.0
CLASSPATH=.:/home/wslogic/jrockit-jdk1.6.0_45-R28.2.7-4.1.0/lib/dt.jar:/home/wslogic/jrockit-jdk1.6.0_45-R28.2.7-4.1.0/lib/tools.jar
USER=wslogic
PWD=/home/wslogic
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/home/wslogic
MAIL=/var/spool/mail/wslogic
SHELL=/bin/bash
TERM=linux
SHLVL=1
LOGNAME=wslogic
PATH=/home/wslogic/middleware/wlserver_10.3/common/bin:/home/wslogic/jrockit-jdk1.6.0_45-R28.2.7-4.1.0/bin:/home/wslogic/Middleware/wlserver_10.3/common/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/dell/srvadmin/bin:/home/wslogic/bin:/home/wslogic/redis/
G_BROKEN_FILENAMES=1
HISTSIZE=1000
CVS_RSH=ssh
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/bin/env
[root@eant ~]#

说明两者的环境不同了,这时有指定的环境变量,所以程序才不会乱码

# linux
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×