Using environment variables with su -c












0















I try to run nvm as a different user from a bash script. It is installed already and the NVM_DIR environment variable is set in /home/user/.bashrc:



export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"


Using plain su works:



root@ubuntu:~# su user
user@:/root$ echo $NVM_DIR
/home/user/.nvm


Trying the same thing with su -c does not work though:



root@ubuntu:~# su user -c 'echo $NVM_DIR'

root@ubuntu:~#


How can I use environment variables with su -c?










share|improve this question

























  • Yes, it is set (only) in user's bashrc

    – thesys
    Feb 14 at 15:08











  • @steeldriver I have extended my question. Using single quotes does not work either.

    – thesys
    Feb 14 at 15:19
















0















I try to run nvm as a different user from a bash script. It is installed already and the NVM_DIR environment variable is set in /home/user/.bashrc:



export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"


Using plain su works:



root@ubuntu:~# su user
user@:/root$ echo $NVM_DIR
/home/user/.nvm


Trying the same thing with su -c does not work though:



root@ubuntu:~# su user -c 'echo $NVM_DIR'

root@ubuntu:~#


How can I use environment variables with su -c?










share|improve this question

























  • Yes, it is set (only) in user's bashrc

    – thesys
    Feb 14 at 15:08











  • @steeldriver I have extended my question. Using single quotes does not work either.

    – thesys
    Feb 14 at 15:19














0












0








0








I try to run nvm as a different user from a bash script. It is installed already and the NVM_DIR environment variable is set in /home/user/.bashrc:



export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"


Using plain su works:



root@ubuntu:~# su user
user@:/root$ echo $NVM_DIR
/home/user/.nvm


Trying the same thing with su -c does not work though:



root@ubuntu:~# su user -c 'echo $NVM_DIR'

root@ubuntu:~#


How can I use environment variables with su -c?










share|improve this question
















I try to run nvm as a different user from a bash script. It is installed already and the NVM_DIR environment variable is set in /home/user/.bashrc:



export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"


Using plain su works:



root@ubuntu:~# su user
user@:/root$ echo $NVM_DIR
/home/user/.nvm


Trying the same thing with su -c does not work though:



root@ubuntu:~# su user -c 'echo $NVM_DIR'

root@ubuntu:~#


How can I use environment variables with su -c?







command-line bash environment-variables su






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 14 at 15:18







thesys

















asked Feb 14 at 14:49









thesysthesys

1117




1117













  • Yes, it is set (only) in user's bashrc

    – thesys
    Feb 14 at 15:08











  • @steeldriver I have extended my question. Using single quotes does not work either.

    – thesys
    Feb 14 at 15:19



















  • Yes, it is set (only) in user's bashrc

    – thesys
    Feb 14 at 15:08











  • @steeldriver I have extended my question. Using single quotes does not work either.

    – thesys
    Feb 14 at 15:19

















Yes, it is set (only) in user's bashrc

– thesys
Feb 14 at 15:08





Yes, it is set (only) in user's bashrc

– thesys
Feb 14 at 15:08













@steeldriver I have extended my question. Using single quotes does not work either.

– thesys
Feb 14 at 15:19





@steeldriver I have extended my question. Using single quotes does not work either.

– thesys
Feb 14 at 15:19










1 Answer
1






active

oldest

votes


















2














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.)






share|improve this answer
























  • What about su - user -c 'echo $NVM_DIR'? With the additional -?

    – PerlDuck
    Feb 14 at 18:52











  • @PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

    – steeldriver
    Feb 14 at 19:05











  • @PerlDuck I also tried that, did not work unfortunately.

    – thesys
    Feb 14 at 19:14











  • @thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

    – PerlDuck
    Feb 14 at 19:22











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1118231%2fusing-environment-variables-with-su-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.)






share|improve this answer
























  • What about su - user -c 'echo $NVM_DIR'? With the additional -?

    – PerlDuck
    Feb 14 at 18:52











  • @PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

    – steeldriver
    Feb 14 at 19:05











  • @PerlDuck I also tried that, did not work unfortunately.

    – thesys
    Feb 14 at 19:14











  • @thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

    – PerlDuck
    Feb 14 at 19:22
















2














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.)






share|improve this answer
























  • What about su - user -c 'echo $NVM_DIR'? With the additional -?

    – PerlDuck
    Feb 14 at 18:52











  • @PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

    – steeldriver
    Feb 14 at 19:05











  • @PerlDuck I also tried that, did not work unfortunately.

    – thesys
    Feb 14 at 19:14











  • @thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

    – PerlDuck
    Feb 14 at 19:22














2












2








2







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.)






share|improve this answer













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.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 14 at 18:31









steeldriversteeldriver

68.9k11113184




68.9k11113184













  • What about su - user -c 'echo $NVM_DIR'? With the additional -?

    – PerlDuck
    Feb 14 at 18:52











  • @PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

    – steeldriver
    Feb 14 at 19:05











  • @PerlDuck I also tried that, did not work unfortunately.

    – thesys
    Feb 14 at 19:14











  • @thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

    – PerlDuck
    Feb 14 at 19:22



















  • What about su - user -c 'echo $NVM_DIR'? With the additional -?

    – PerlDuck
    Feb 14 at 18:52











  • @PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

    – steeldriver
    Feb 14 at 19:05











  • @PerlDuck I also tried that, did not work unfortunately.

    – thesys
    Feb 14 at 19:14











  • @thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

    – PerlDuck
    Feb 14 at 19:22

















What about su - user -c 'echo $NVM_DIR'? With the additional -?

– PerlDuck
Feb 14 at 18:52





What about su - user -c 'echo $NVM_DIR'? With the additional -?

– PerlDuck
Feb 14 at 18:52













@PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

– steeldriver
Feb 14 at 19:05





@PerlDuck that will force it to use a login shell, which - if the user's login shell is bash, at least with the default Ubuntu ~/.profile, will read their ~/.bashrc: however, the default ~/.bashrc basically just exits if the shell is non-interactive. It would work if the environment variables were defined before the interactivity test - but that feels kind of wrong somehow.

– steeldriver
Feb 14 at 19:05













@PerlDuck I also tried that, did not work unfortunately.

– thesys
Feb 14 at 19:14





@PerlDuck I also tried that, did not work unfortunately.

– thesys
Feb 14 at 19:14













@thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

– PerlDuck
Feb 14 at 19:22





@thesys Okay. Was just an idea. But steeldrivers explanation and suggestion seems reasonable.

– PerlDuck
Feb 14 at 19:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1118231%2fusing-environment-variables-with-su-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕