Using environment variables with su -c
The behavior you're observing is because su -c 'command'
does not execute command
in an interactive shell.
Since su
doesn't appear to provide an option to do so, probably the cleanest way to do what you want is to move the NVM environment variables into a separate file, called nvm_envs
say, and source that explicitly e.g.
su user -c '. ~/nvm_envs && echo $NVM_DIR'
If you want interactive bash shells to have the same environment, source the same file from your .bashrc
file e.g.
if [ -r "$HOME/nvm_envs" ]; then
. "$HOME/nvm_envs"
fi
(This is exactly how the default .profile
file conditionally sources the user's env_vars
file: you could use that file instead if you prefer.)