Coincidentally, in the morning, I mentioned the problem of su switching users. Several developers have asked this before, so take a note:
Difference between su and su -
-
su just switches the user identity, but the shell environment is still the shell of the previous user, su without - will not read the environment configuration file of the target user
-
su - The user and the shell environment are switched together, and the environment configuration file under the target user will be loaded, such as the .bashrc file
[root ~]# whoami
root
[root ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root ~]#
Without -, after switching to the oracle user, the PATH variable is still the same as root
[root ~]# su oracle
[oracle root]$ echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
After adding -, it is the value of the oracle user's own PATH variable
[root@erptest1 ~]# su - oracle
[oracle@erptest1 ~]$ echo $PATH
/opt/app/oracle/product/11.2.0/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/home/oracle/bin:/opt/app/oracle/product/11.2.0/lib:/opt/app/oracle/product/11.2.0/jdk:/opt/app/oracle/product/11.2.0/lib32:/usr/lib64:/opt/app/oracle/product/11.2.0:/opt/app/oracle/product/11.2.0/ctx/lib
[root@erptest1 ~]# echo $ORACLE_HOME
[root@erptest1 ~]#
[root@erptest1 ~]# su oracle
[oracle@erptest1 root]$ echo $ORACLE_HOME
[oracle@erptest1 root]$
[root@erptest1 ~]# su - oracle
[oracle@erptest1 ~]$ echo $ORACLE_HOME
/opt/app/oracle/product/11.2.0
[oracle@erptest1 root]$