Change user and load entire environment in shell script
Using sudo su - user -c
will make it as though you logged in as that person inheriting their path. That's why su user
is different from su - user
.
However if you are setting the path in a .bashrc
file it will only take effect for interactive sessions (i.e. ssh-ing in as that user).
You should instead use .bash_profile
or /etc/profile
, see this.
For the login context use either:
sudo -i -u USER COMMAND
su -l USER -c COMMAND