sudo -u user bash works but $HOME is not changing accordingly
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I'm writing a script to setup new debian install's.
The problem is in this code:
if [ ! -z "$USER1" ]
then
sudo -u "$USER1" bash <<-EOF
cp "$BASHRC $HOME"/.bashrc
wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
. "$HOME"/.bashrc
echo "Here is LS_COLORS in action: "
ls -l "$HOME"/
EOF
PROBLEM:
When I sudo to another user the $HOME variable is not showing/changing this new users home directory.
QUESTIONS:
Can you explain to me the problem?
How would I do this in the best and efficient way?
Edit:
Why do I keep getting "cp: cannot stat '': No such file or directory
" or "cp: missing opereand
" in my cp command? I think it has something to do with $HOME again..........
bash shell-script environment-variables home programming
add a comment |Â
up vote
2
down vote
favorite
I'm writing a script to setup new debian install's.
The problem is in this code:
if [ ! -z "$USER1" ]
then
sudo -u "$USER1" bash <<-EOF
cp "$BASHRC $HOME"/.bashrc
wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
. "$HOME"/.bashrc
echo "Here is LS_COLORS in action: "
ls -l "$HOME"/
EOF
PROBLEM:
When I sudo to another user the $HOME variable is not showing/changing this new users home directory.
QUESTIONS:
Can you explain to me the problem?
How would I do this in the best and efficient way?
Edit:
Why do I keep getting "cp: cannot stat '': No such file or directory
" or "cp: missing opereand
" in my cp command? I think it has something to do with $HOME again..........
bash shell-script environment-variables home programming
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm writing a script to setup new debian install's.
The problem is in this code:
if [ ! -z "$USER1" ]
then
sudo -u "$USER1" bash <<-EOF
cp "$BASHRC $HOME"/.bashrc
wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
. "$HOME"/.bashrc
echo "Here is LS_COLORS in action: "
ls -l "$HOME"/
EOF
PROBLEM:
When I sudo to another user the $HOME variable is not showing/changing this new users home directory.
QUESTIONS:
Can you explain to me the problem?
How would I do this in the best and efficient way?
Edit:
Why do I keep getting "cp: cannot stat '': No such file or directory
" or "cp: missing opereand
" in my cp command? I think it has something to do with $HOME again..........
bash shell-script environment-variables home programming
I'm writing a script to setup new debian install's.
The problem is in this code:
if [ ! -z "$USER1" ]
then
sudo -u "$USER1" bash <<-EOF
cp "$BASHRC $HOME"/.bashrc
wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
. "$HOME"/.bashrc
echo "Here is LS_COLORS in action: "
ls -l "$HOME"/
EOF
PROBLEM:
When I sudo to another user the $HOME variable is not showing/changing this new users home directory.
QUESTIONS:
Can you explain to me the problem?
How would I do this in the best and efficient way?
Edit:
Why do I keep getting "cp: cannot stat '': No such file or directory
" or "cp: missing opereand
" in my cp command? I think it has something to do with $HOME again..........
bash shell-script environment-variables home programming
edited Aug 18 at 3:41
asked Aug 17 at 23:19
somethingSomething
1,51992954
1,51992954
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
The here-doc content is evaluated BEFORE the sudo
is called. This means that every instance of $HOME
is in the context of the caller, not the sudo
. You can see this in action here:
A=apple
bash <<-EOF
A=banana
echo "$A"
EOF
The output is
apple
If you quote your EOF
marker its contents will be quoted correspondingly:
A=apple
bash <<-'EOF'
A=banana
echo "$A"
EOF
Output
banana
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
The here-doc content is evaluated BEFORE the sudo
is called. This means that every instance of $HOME
is in the context of the caller, not the sudo
. You can see this in action here:
A=apple
bash <<-EOF
A=banana
echo "$A"
EOF
The output is
apple
If you quote your EOF
marker its contents will be quoted correspondingly:
A=apple
bash <<-'EOF'
A=banana
echo "$A"
EOF
Output
banana
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
add a comment |Â
up vote
5
down vote
accepted
The here-doc content is evaluated BEFORE the sudo
is called. This means that every instance of $HOME
is in the context of the caller, not the sudo
. You can see this in action here:
A=apple
bash <<-EOF
A=banana
echo "$A"
EOF
The output is
apple
If you quote your EOF
marker its contents will be quoted correspondingly:
A=apple
bash <<-'EOF'
A=banana
echo "$A"
EOF
Output
banana
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
The here-doc content is evaluated BEFORE the sudo
is called. This means that every instance of $HOME
is in the context of the caller, not the sudo
. You can see this in action here:
A=apple
bash <<-EOF
A=banana
echo "$A"
EOF
The output is
apple
If you quote your EOF
marker its contents will be quoted correspondingly:
A=apple
bash <<-'EOF'
A=banana
echo "$A"
EOF
Output
banana
The here-doc content is evaluated BEFORE the sudo
is called. This means that every instance of $HOME
is in the context of the caller, not the sudo
. You can see this in action here:
A=apple
bash <<-EOF
A=banana
echo "$A"
EOF
The output is
apple
If you quote your EOF
marker its contents will be quoted correspondingly:
A=apple
bash <<-'EOF'
A=banana
echo "$A"
EOF
Output
banana
answered Aug 17 at 23:47
roaima
39.7k545108
39.7k545108
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
add a comment |Â
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
wow that's great it runs correctly now
â somethingSomething
Aug 18 at 1:09
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463282%2fsudo-u-user-bash-works-but-home-is-not-changing-accordingly%23new-answer', 'question_page');
);
Post as a guest
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
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
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