execute command with sudo and execute Bash script with sudo

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I was always thinking that to execute commands with sudo and to execute bash script with sudo were the same. But today I find that they are different. Here is what I've tested:



me@my_machine:~$ sudo echo $(whoami)


I execute the command from terminal and get the output: me. After reading this link: https://superuser.com/questions/384544/why-does-sudo-u-root-echo-whoami-not-return-root, I understand this.



Then I create a bash script named test.sh:



#!/usr/bin/env bash
echo $(whoami)


And I execute this script: sudo ./test.sh, to my surprise, the output now becomes: root.



So executing commands with sudo and executing Bash scripts with sudo are different?



If I have s Bash script, containing the command whoami, have it be executed with sudo, how could I get me, instead of root?







share|improve this question



























    up vote
    1
    down vote

    favorite












    I was always thinking that to execute commands with sudo and to execute bash script with sudo were the same. But today I find that they are different. Here is what I've tested:



    me@my_machine:~$ sudo echo $(whoami)


    I execute the command from terminal and get the output: me. After reading this link: https://superuser.com/questions/384544/why-does-sudo-u-root-echo-whoami-not-return-root, I understand this.



    Then I create a bash script named test.sh:



    #!/usr/bin/env bash
    echo $(whoami)


    And I execute this script: sudo ./test.sh, to my surprise, the output now becomes: root.



    So executing commands with sudo and executing Bash scripts with sudo are different?



    If I have s Bash script, containing the command whoami, have it be executed with sudo, how could I get me, instead of root?







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I was always thinking that to execute commands with sudo and to execute bash script with sudo were the same. But today I find that they are different. Here is what I've tested:



      me@my_machine:~$ sudo echo $(whoami)


      I execute the command from terminal and get the output: me. After reading this link: https://superuser.com/questions/384544/why-does-sudo-u-root-echo-whoami-not-return-root, I understand this.



      Then I create a bash script named test.sh:



      #!/usr/bin/env bash
      echo $(whoami)


      And I execute this script: sudo ./test.sh, to my surprise, the output now becomes: root.



      So executing commands with sudo and executing Bash scripts with sudo are different?



      If I have s Bash script, containing the command whoami, have it be executed with sudo, how could I get me, instead of root?







      share|improve this question













      I was always thinking that to execute commands with sudo and to execute bash script with sudo were the same. But today I find that they are different. Here is what I've tested:



      me@my_machine:~$ sudo echo $(whoami)


      I execute the command from terminal and get the output: me. After reading this link: https://superuser.com/questions/384544/why-does-sudo-u-root-echo-whoami-not-return-root, I understand this.



      Then I create a bash script named test.sh:



      #!/usr/bin/env bash
      echo $(whoami)


      And I execute this script: sudo ./test.sh, to my surprise, the output now becomes: root.



      So executing commands with sudo and executing Bash scripts with sudo are different?



      If I have s Bash script, containing the command whoami, have it be executed with sudo, how could I get me, instead of root?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 28 at 21:18









      slm♦

      232k65479649




      232k65479649









      asked Jul 28 at 10:59









      Yves

      693414




      693414




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          For the shell to run the command



          sudo echo $(whoami)


          it must first figure out what arguments to call sudo with. echo is just a simple string, so the shell does nothing to it, but $(whoami) is a command substitution that needs to be expanded. It does this by executing whoami and replacing the command substitution with its output. It is then ready to call sudo.



          This means that sudo will be executed with echo and the output of whoami (which was executed as your unprivileged user) as arguments.



          If you want to run whoami with sudo, you can put it in a script and run the script with sudo, as you've done, or you may use



          sudo sh -c 'echo "$(whoami)"'


          Here, the sh -c script is run as root, so whoami will also run as root.



          The point is that the shell that runs whoami is executing as the root user.



          The shell that executes whoami in the first command, sudo echo $(whoami), runs as your ordinary user, not root.






          share|improve this answer























            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%2f459029%2fexecute-command-with-sudo-and-execute-bash-script-with-sudo%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
            3
            down vote



            accepted










            For the shell to run the command



            sudo echo $(whoami)


            it must first figure out what arguments to call sudo with. echo is just a simple string, so the shell does nothing to it, but $(whoami) is a command substitution that needs to be expanded. It does this by executing whoami and replacing the command substitution with its output. It is then ready to call sudo.



            This means that sudo will be executed with echo and the output of whoami (which was executed as your unprivileged user) as arguments.



            If you want to run whoami with sudo, you can put it in a script and run the script with sudo, as you've done, or you may use



            sudo sh -c 'echo "$(whoami)"'


            Here, the sh -c script is run as root, so whoami will also run as root.



            The point is that the shell that runs whoami is executing as the root user.



            The shell that executes whoami in the first command, sudo echo $(whoami), runs as your ordinary user, not root.






            share|improve this answer



























              up vote
              3
              down vote



              accepted










              For the shell to run the command



              sudo echo $(whoami)


              it must first figure out what arguments to call sudo with. echo is just a simple string, so the shell does nothing to it, but $(whoami) is a command substitution that needs to be expanded. It does this by executing whoami and replacing the command substitution with its output. It is then ready to call sudo.



              This means that sudo will be executed with echo and the output of whoami (which was executed as your unprivileged user) as arguments.



              If you want to run whoami with sudo, you can put it in a script and run the script with sudo, as you've done, or you may use



              sudo sh -c 'echo "$(whoami)"'


              Here, the sh -c script is run as root, so whoami will also run as root.



              The point is that the shell that runs whoami is executing as the root user.



              The shell that executes whoami in the first command, sudo echo $(whoami), runs as your ordinary user, not root.






              share|improve this answer

























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                For the shell to run the command



                sudo echo $(whoami)


                it must first figure out what arguments to call sudo with. echo is just a simple string, so the shell does nothing to it, but $(whoami) is a command substitution that needs to be expanded. It does this by executing whoami and replacing the command substitution with its output. It is then ready to call sudo.



                This means that sudo will be executed with echo and the output of whoami (which was executed as your unprivileged user) as arguments.



                If you want to run whoami with sudo, you can put it in a script and run the script with sudo, as you've done, or you may use



                sudo sh -c 'echo "$(whoami)"'


                Here, the sh -c script is run as root, so whoami will also run as root.



                The point is that the shell that runs whoami is executing as the root user.



                The shell that executes whoami in the first command, sudo echo $(whoami), runs as your ordinary user, not root.






                share|improve this answer















                For the shell to run the command



                sudo echo $(whoami)


                it must first figure out what arguments to call sudo with. echo is just a simple string, so the shell does nothing to it, but $(whoami) is a command substitution that needs to be expanded. It does this by executing whoami and replacing the command substitution with its output. It is then ready to call sudo.



                This means that sudo will be executed with echo and the output of whoami (which was executed as your unprivileged user) as arguments.



                If you want to run whoami with sudo, you can put it in a script and run the script with sudo, as you've done, or you may use



                sudo sh -c 'echo "$(whoami)"'


                Here, the sh -c script is run as root, so whoami will also run as root.



                The point is that the shell that runs whoami is executing as the root user.



                The shell that executes whoami in the first command, sudo echo $(whoami), runs as your ordinary user, not root.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jul 28 at 11:39


























                answered Jul 28 at 11:17









                Kusalananda

                101k13199311




                101k13199311






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459029%2fexecute-command-with-sudo-and-execute-bash-script-with-sudo%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay