I am struggling with this cp always giving errors because the variables not there...

Here is my code:

if [ ! -z "$USER1" ]
     sudo -u "$USER1" bash <<'EOF'
     sleep 5
     cp "$BASHRC" "$HOME"/.bashrc
     sleep 5
     wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
     sleep 5
     echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
     . "$HOME"/.bashrc
     sleep 5
     echo "Here is LS_COLORS in action: "
     ls -l "$HOME"


How to make my script-varibles and environmental variables accessable inside the sudo clauses(there are many)?

Is it sudo or is it here-document that has a problem with this?


Removing the quotes on EOF gives me a previous problem:

./testing3892739.sh billy
cp: failed to access '/root/.bashrc': Permission denied


Now the script looks like this.....

    if [ ! -z "$USER1" ]
108 then
109     sudo -i -u "$USER1" bash <<'EOF'
110     sleep 5
111     cp -f .bashrc "$HOME"/.bashrc
112     sleep 5
113     wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
114     sleep 5
115     echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
116     . "$HOME"/.bashrc
117     sleep 5
118     echo "Here is LS_COLORS in action: "
119     ls -l "$HOME"
120 EOF
121 fi

the -i switch to sudo made the difference on the last problem.... but as usual and for some time now and many different errors, cp gives an error and now it is

cp: '.bashrc' and '/home/billy/.bashrc' are the same file

The file exist's but I need it to be overwritten.

I simply want my script to cp the hidden file .bashrc from current dir which can be anywhere because this is the dir the script resides and files needed for the update of the server, into $HOME and overwrite if it's existing.

  • 1
    Bearing in mind your previous question sudo -u user bash works but $HOME is not changing accordingly I assume you want $HOME to expand to $USER1's home directory - to achieve that, you probably need to invoke a login shell by adding -i or --login to your sudo invocation. – steeldriver Aug 18 '18 at 23:11
  • @steeldriver thanks man.......great resource for assitstance this site I just love this place – somethingSomething Aug 19 '18 at 0:56
  • @steeldriver then I get this error cp: '.bashrc' and '/home/billy/.bashrc' are the same file no problem when I do this for root – somethingSomething Aug 19 '18 at 1:09
  • Maybe take a step back and explain what you want your code to do? – steeldriver Aug 19 '18 at 1:12
  • @steeldriver Ok I will take some breathers and a coffee and write a edit what's going on with the script, your input solved the former problem but I'm still not over the hill where the grass is greener – somethingSomething Aug 19 '18 at 1:18

So (based on comments) you want .bashrc to refer to a file in the directory from which sudo is invoked; but $HOME to expand to the home directory of the target user. I think the way to do that is to use -H:

 -H, --set-home
             Request that the security policy set the HOME environment
             variable to the home directory specified by the target user's
             password database entry.  Depending on the policy, this may
             be the default behavior.

To illustrate:

$ sudo -Hu testuser bash -c 'echo \$PWD = $PWD; echo \$HOME = $HOME' 
$PWD = /home/steeldriver/forums/tests
$HOME = /home/testuser

whereas with "regular" sudo $HOME resolves to that of the invoking user

$ sudo -u testuser bash -c 'echo \$PWD = $PWD; echo \$HOME = $HOME'
$PWD = /home/steeldriver/forums/tests
$HOME = /home/steeldriver

while with a login shell $HOME resolves to that of the target user - but the working directory changes there as well:

$ sudo -iu testuser bash -c 'echo \$PWD = $PWD; echo \$HOME = $HOME'
/home/testuser = /home/testuser
/home/testuser = /home/testuser

neither of which are very useful in your application.

Bear in mind that sudo is quite configurable and the default behavior may be different depending on your precise flavor and distribution of Unix/Linux.

| improve this answer | |
  • A little misunderstanding is that the . is for hidden file in the current dir – somethingSomething Aug 19 '18 at 2:52
  • @somethingSomething doh - yes of course, been a long day ;) – steeldriver Aug 19 '18 at 2:54
  • Apologies for not being clear enough....... please – somethingSomething Aug 19 '18 at 2:57
  • Wow I just understood the logic, hahaha your the best thanks – somethingSomething Aug 19 '18 at 3:09

You are using <<'EOF'. If you use <<EOF without the quotes, your shell will expand the variables. If there are variables you don't want expanded by your shell, quote them with \$.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.