su & sudo -Switch User and running commands as others

su

su [username] #change user ID or become superuser
-             #A hyphen is used to provide an environment similar to what the user would expect had the user logged in directly.
-c command    #Specify a command to be executed.

whoami #Displays the effective username.

#Example
$export TEST=1
$echo $TEST   
1
$su oracle 
$whoami
oracle
$echo $TEST  #Show 1 here, still in current account's environment
1
$exit
su - oracle
$echo $TEST  #show nothing here, because now in oracle's environment 

$pwd
/home/oracle 


#Example
$su -c 'echo $ORACLE_HOME' oracle #nothing return
$su -c 'echo $ORACLE_HOME' - oracle #show something
/u01/app/oracle/product/current

sudo

Last updated