Using environment variables with su -c
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
add a comment |
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
Yes, it is set (only) inuser
'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
add a comment |
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
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
command-line bash environment-variables su
edited Feb 14 at 15:18
thesys
asked Feb 14 at 14:49
thesysthesys
1117
1117
Yes, it is set (only) inuser
'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
add a comment |
Yes, it is set (only) inuser
'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
add a comment |
1 Answer
1
active
oldest
votes
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.)
What aboutsu - 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 isbash
, 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.)
What aboutsu - 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 isbash
, 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
add a comment |
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.)
What aboutsu - 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 isbash
, 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
add a comment |
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.)
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.)
answered Feb 14 at 18:31
steeldriversteeldriver
68.9k11113184
68.9k11113184
What aboutsu - 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 isbash
, 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
add a comment |
What aboutsu - 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 isbash
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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