sudo -u user bash works but $HOME is not changing accordingly

The name of the pictureThe name of the pictureThe name of the pictureClash 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..........







share|improve this question


























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







    share|improve this question
























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







      share|improve this question














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









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 18 at 3:41

























      asked Aug 17 at 23:19









      somethingSomething

      1,51992954




      1,51992954




















          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





          share|improve this answer




















          • wow that's great it runs correctly now
            – somethingSomething
            Aug 18 at 1:09










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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





          share|improve this answer




















          • wow that's great it runs correctly now
            – somethingSomething
            Aug 18 at 1:09














          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





          share|improve this answer




















          • wow that's great it runs correctly now
            – somethingSomething
            Aug 18 at 1:09












          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Mutual Information Always Non-negative

          Why am i infinitely getting the same tweet with the Twitter Search API?