which shell will sudo use to execute a shell script without the shebang lineLoops not working in bash when...

Why do balloons get cold when they deflate?

Why should care be taken while closing a capacitive circuit?

Check disk usage of files returned with spaces

Is recepted a word?

Polar contour plot in Mathematica?

How do we test and determine if a USB cable+connector is version 2, 3.0 or 3.1?

Unsolved Problems due to Lack of Computational Power

My new Acer Aspire 7 doesn't have a Legacy Boot option, what can I do to get it?

Are unaudited server logs admissible in a court of law?

Playing a fast but quiet Alberti bass

Can 'in-' mean both 'in' and 'no'?

Is there a commercial liquid with refractive index greater than n=2?

How best to join tables, which have different lengths on the same column values which exist in both tables?

What are these protruding elements from SU-27's tail?

Do banks' profitability really suffer under low interest rates

Is there a way to make the "o" keypress of other-window <C-x><C-o> repeatable?

How can I train a replacement without them knowing?

Vegetarian dishes on Russian trains (European part)

Can others monetize my project with GPLv3?

Number of matrices with bounded products of rows and columns

When does The Truman Show take place?

What's the point of writing that I know will never be used or read?

Does git delete empty folders?

How to detect a failed AES256 decryption programmatically?



which shell will sudo use to execute a shell script without the shebang line


Loops not working in bash when bash script is run with sudo on UbuntuIf I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?How to prevent the caller's shell from being used in sudoInvoking a script with sudo ignores the shebangdebian 8.2 can no longer su to rootWhich shell interpreter runs a script with no shebang?how to let sudo fork bash instead of sh?execute command with sudo and execute Bash script with sudoHow does a EC2 machine prevents ssh root access with a warning?Execute shell script without passwordsudo without sudo, implying sudo in script






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







7















My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1.



First I login as a normal user:



$ whoami
fin

$ cat /etc/passwd | grep -i "root|fin"
root:x:0:0:root:/root:/bin/bash
fin:x:1000:1000:This is a normal user:/home/fin:/bin/bash

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh -> dash

$ ls -l /bin/bash
-rwxr-xr-x 1 root root 920788 Apr 3 2012 /bin/bash

$ echo $SHELL
/bin/bash

$ ps | grep "$$" | awk '{ print $4 }'
bash

$ ls -l ./test.sh
-rwxr-xr-x 1 fin fin 37 Sep 27 16:46 test.sh

$ cat ./test.sh
ps | grep "$$" | awk '{ print $4 }'

$ ./test.sh
bash

$ sudo ./test.sh
sh


I suppose the last output should also be bash because /etc/passwd shows that root uses bash, am I missing any points about sudo?










share|improve this question

































    7















    My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1.



    First I login as a normal user:



    $ whoami
    fin

    $ cat /etc/passwd | grep -i "root|fin"
    root:x:0:0:root:/root:/bin/bash
    fin:x:1000:1000:This is a normal user:/home/fin:/bin/bash

    $ ls -l /bin/sh
    lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh -> dash

    $ ls -l /bin/bash
    -rwxr-xr-x 1 root root 920788 Apr 3 2012 /bin/bash

    $ echo $SHELL
    /bin/bash

    $ ps | grep "$$" | awk '{ print $4 }'
    bash

    $ ls -l ./test.sh
    -rwxr-xr-x 1 fin fin 37 Sep 27 16:46 test.sh

    $ cat ./test.sh
    ps | grep "$$" | awk '{ print $4 }'

    $ ./test.sh
    bash

    $ sudo ./test.sh
    sh


    I suppose the last output should also be bash because /etc/passwd shows that root uses bash, am I missing any points about sudo?










    share|improve this question





























      7












      7








      7








      My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1.



      First I login as a normal user:



      $ whoami
      fin

      $ cat /etc/passwd | grep -i "root|fin"
      root:x:0:0:root:/root:/bin/bash
      fin:x:1000:1000:This is a normal user:/home/fin:/bin/bash

      $ ls -l /bin/sh
      lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh -> dash

      $ ls -l /bin/bash
      -rwxr-xr-x 1 root root 920788 Apr 3 2012 /bin/bash

      $ echo $SHELL
      /bin/bash

      $ ps | grep "$$" | awk '{ print $4 }'
      bash

      $ ls -l ./test.sh
      -rwxr-xr-x 1 fin fin 37 Sep 27 16:46 test.sh

      $ cat ./test.sh
      ps | grep "$$" | awk '{ print $4 }'

      $ ./test.sh
      bash

      $ sudo ./test.sh
      sh


      I suppose the last output should also be bash because /etc/passwd shows that root uses bash, am I missing any points about sudo?










      share|improve this question
















      My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1.



      First I login as a normal user:



      $ whoami
      fin

      $ cat /etc/passwd | grep -i "root|fin"
      root:x:0:0:root:/root:/bin/bash
      fin:x:1000:1000:This is a normal user:/home/fin:/bin/bash

      $ ls -l /bin/sh
      lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh -> dash

      $ ls -l /bin/bash
      -rwxr-xr-x 1 root root 920788 Apr 3 2012 /bin/bash

      $ echo $SHELL
      /bin/bash

      $ ps | grep "$$" | awk '{ print $4 }'
      bash

      $ ls -l ./test.sh
      -rwxr-xr-x 1 fin fin 37 Sep 27 16:46 test.sh

      $ cat ./test.sh
      ps | grep "$$" | awk '{ print $4 }'

      $ ./test.sh
      bash

      $ sudo ./test.sh
      sh


      I suppose the last output should also be bash because /etc/passwd shows that root uses bash, am I missing any points about sudo?







      bash shell shell-script sudo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 27 '13 at 9:26









      slm

      266k74 gold badges576 silver badges722 bronze badges




      266k74 gold badges576 silver badges722 bronze badges










      asked Sep 27 '13 at 9:02









      laykalayka

      481 silver badge4 bronze badges




      481 silver badge4 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          12














          It uses _PATH_BSHELL like execvp() which on Linux is defined as /bin/sh in /usr/include/paths.h. That should be the same as when executed with env or find -exec for instance.



          It should certainly not use the user's login shell. The fact that you're seeing bash above is because it's bash (the shell you enter that command line in) that tries to execute it and when it gets a ENOEXEC error code from execve it decides to interpret it with itself instead (in sh compatibility mode).






          share|improve this answer




























          • Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

            – layka
            Sep 27 '13 at 14:11











          • No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

            – Stéphane Chazelas
            Sep 27 '13 at 14:19











          • @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

            – cuonglm
            Sep 27 '13 at 14:31











          • @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

            – Stéphane Chazelas
            Sep 27 '13 at 17:43





















          5














          Because you don't use -s option, so sudo will use _PATH_BSHELL (which is define in /usr/include/paths.h on Ubuntu 12.04 LTS) to set $SHELL it run. Looking in to sudo source code:



          /* Stash user's shell for use with the -s flag; don't pass to plugin. */
          if ((ud->shell = getenv("SHELL")) == NULL || ud->shell[0] == '') {
          ud->shell = pw->pw_shell[0] ? pw->pw_shell : _PATH_BSHELL;
          }


          If you use -s option, sudo will use your $SHELL instead of _PATH_BSHELL:



          $ cat ./test.sh
          ps | grep "$$" | awk '{ print $4 }'

          $ ./test.sh
          bash

          $ sudo -s ./test.sh
          bash





          share|improve this answer





















          • 1





            Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

            – Stéphane Chazelas
            Sep 27 '13 at 11:05













          • In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

            – Stéphane Chazelas
            Sep 27 '13 at 12:21



















          4














          The kernel can only run binary executable images. So how do scripts get run? After all, I can type my_script_without_shebang at a shell prompt and I don't get an ENOEXEC error. Script execution is done not by the kernel, but by the shell. The exec code in the shell usually looks something like:



          /* try to run the program */
          execl(program, basename(program), (char *)0);

          /* the exec failed -- maybe it is a shell script without shebang? */
          if (errno == ENOEXEC)
          execl ("/bin/sh", "sh", "-c", program, (char *)0);


          You can verify that with tracing a dummy shell script without shebang:



          cat > /tmp/foo.sh <<EOF
          echo
          EOF

          chmod u+x /tmp/foo.sh

          strace /tmp/foo.sh 2>&1 | grep exec
          execve("/tmp/foo.sh", ["/tmp/foo.sh"], [/* 28 vars */]) = -1 ENOEXEC (Exec format error)


          So then, the execution proceed as Stephane described - default shell is used (in the above code snippet is hard-coded). This nice UNIX FAQ can answer more.






          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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f92622%2fwhich-shell-will-sudo-use-to-execute-a-shell-script-without-the-shebang-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            12














            It uses _PATH_BSHELL like execvp() which on Linux is defined as /bin/sh in /usr/include/paths.h. That should be the same as when executed with env or find -exec for instance.



            It should certainly not use the user's login shell. The fact that you're seeing bash above is because it's bash (the shell you enter that command line in) that tries to execute it and when it gets a ENOEXEC error code from execve it decides to interpret it with itself instead (in sh compatibility mode).






            share|improve this answer




























            • Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

              – layka
              Sep 27 '13 at 14:11











            • No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

              – Stéphane Chazelas
              Sep 27 '13 at 14:19











            • @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

              – cuonglm
              Sep 27 '13 at 14:31











            • @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

              – Stéphane Chazelas
              Sep 27 '13 at 17:43


















            12














            It uses _PATH_BSHELL like execvp() which on Linux is defined as /bin/sh in /usr/include/paths.h. That should be the same as when executed with env or find -exec for instance.



            It should certainly not use the user's login shell. The fact that you're seeing bash above is because it's bash (the shell you enter that command line in) that tries to execute it and when it gets a ENOEXEC error code from execve it decides to interpret it with itself instead (in sh compatibility mode).






            share|improve this answer




























            • Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

              – layka
              Sep 27 '13 at 14:11











            • No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

              – Stéphane Chazelas
              Sep 27 '13 at 14:19











            • @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

              – cuonglm
              Sep 27 '13 at 14:31











            • @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

              – Stéphane Chazelas
              Sep 27 '13 at 17:43
















            12












            12








            12







            It uses _PATH_BSHELL like execvp() which on Linux is defined as /bin/sh in /usr/include/paths.h. That should be the same as when executed with env or find -exec for instance.



            It should certainly not use the user's login shell. The fact that you're seeing bash above is because it's bash (the shell you enter that command line in) that tries to execute it and when it gets a ENOEXEC error code from execve it decides to interpret it with itself instead (in sh compatibility mode).






            share|improve this answer















            It uses _PATH_BSHELL like execvp() which on Linux is defined as /bin/sh in /usr/include/paths.h. That should be the same as when executed with env or find -exec for instance.



            It should certainly not use the user's login shell. The fact that you're seeing bash above is because it's bash (the shell you enter that command line in) that tries to execute it and when it gets a ENOEXEC error code from execve it decides to interpret it with itself instead (in sh compatibility mode).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 27 '13 at 11:00

























            answered Sep 27 '13 at 9:51









            Stéphane ChazelasStéphane Chazelas

            330k58 gold badges642 silver badges1009 bronze badges




            330k58 gold badges642 silver badges1009 bronze badges
















            • Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

              – layka
              Sep 27 '13 at 14:11











            • No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

              – Stéphane Chazelas
              Sep 27 '13 at 14:19











            • @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

              – cuonglm
              Sep 27 '13 at 14:31











            • @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

              – Stéphane Chazelas
              Sep 27 '13 at 17:43





















            • Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

              – layka
              Sep 27 '13 at 14:11











            • No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

              – Stéphane Chazelas
              Sep 27 '13 at 14:19











            • @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

              – cuonglm
              Sep 27 '13 at 14:31











            • @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

              – Stéphane Chazelas
              Sep 27 '13 at 17:43



















            Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

            – layka
            Sep 27 '13 at 14:11





            Thanks for your reply, so it depends on sudo implementation to decide whether to use sh or parse /etc/passwd when it receieves ENOEXEC error?

            – layka
            Sep 27 '13 at 14:11













            No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

            – Stéphane Chazelas
            Sep 27 '13 at 14:19





            No, sudo will not use the user's login shell unless you tell it to by telling it you want to pass it a command line to interpret by a shell (with -s) as opposed to a command to execute. If a command to execute, it will behave like execvp, that is like the system is meant to execute commands.

            – Stéphane Chazelas
            Sep 27 '13 at 14:19













            @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

            – cuonglm
            Sep 27 '13 at 14:31





            @Stephane Chazelas: I think it doesn't mean system to run command. sudo always setup its execution enviroment prior the call to execve(). If you don't pass -s, sudo will use _PATH_BSHELL.

            – cuonglm
            Sep 27 '13 at 14:31













            @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

            – Stéphane Chazelas
            Sep 27 '13 at 17:43







            @Gnouc, Yes, sudo will use _PATH_BSHELL like the system does (in execvp(), env, find, vi most shells...), it just happen to implement its own version of execvp(). It may also use _PATH_BSHELL with -s if executing $SHELL returns ENOEXEC. See my comment to your answer for more details.

            – Stéphane Chazelas
            Sep 27 '13 at 17:43















            5














            Because you don't use -s option, so sudo will use _PATH_BSHELL (which is define in /usr/include/paths.h on Ubuntu 12.04 LTS) to set $SHELL it run. Looking in to sudo source code:



            /* Stash user's shell for use with the -s flag; don't pass to plugin. */
            if ((ud->shell = getenv("SHELL")) == NULL || ud->shell[0] == '') {
            ud->shell = pw->pw_shell[0] ? pw->pw_shell : _PATH_BSHELL;
            }


            If you use -s option, sudo will use your $SHELL instead of _PATH_BSHELL:



            $ cat ./test.sh
            ps | grep "$$" | awk '{ print $4 }'

            $ ./test.sh
            bash

            $ sudo -s ./test.sh
            bash





            share|improve this answer





















            • 1





              Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

              – Stéphane Chazelas
              Sep 27 '13 at 11:05













            • In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

              – Stéphane Chazelas
              Sep 27 '13 at 12:21
















            5














            Because you don't use -s option, so sudo will use _PATH_BSHELL (which is define in /usr/include/paths.h on Ubuntu 12.04 LTS) to set $SHELL it run. Looking in to sudo source code:



            /* Stash user's shell for use with the -s flag; don't pass to plugin. */
            if ((ud->shell = getenv("SHELL")) == NULL || ud->shell[0] == '') {
            ud->shell = pw->pw_shell[0] ? pw->pw_shell : _PATH_BSHELL;
            }


            If you use -s option, sudo will use your $SHELL instead of _PATH_BSHELL:



            $ cat ./test.sh
            ps | grep "$$" | awk '{ print $4 }'

            $ ./test.sh
            bash

            $ sudo -s ./test.sh
            bash





            share|improve this answer





















            • 1





              Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

              – Stéphane Chazelas
              Sep 27 '13 at 11:05













            • In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

              – Stéphane Chazelas
              Sep 27 '13 at 12:21














            5












            5








            5







            Because you don't use -s option, so sudo will use _PATH_BSHELL (which is define in /usr/include/paths.h on Ubuntu 12.04 LTS) to set $SHELL it run. Looking in to sudo source code:



            /* Stash user's shell for use with the -s flag; don't pass to plugin. */
            if ((ud->shell = getenv("SHELL")) == NULL || ud->shell[0] == '') {
            ud->shell = pw->pw_shell[0] ? pw->pw_shell : _PATH_BSHELL;
            }


            If you use -s option, sudo will use your $SHELL instead of _PATH_BSHELL:



            $ cat ./test.sh
            ps | grep "$$" | awk '{ print $4 }'

            $ ./test.sh
            bash

            $ sudo -s ./test.sh
            bash





            share|improve this answer













            Because you don't use -s option, so sudo will use _PATH_BSHELL (which is define in /usr/include/paths.h on Ubuntu 12.04 LTS) to set $SHELL it run. Looking in to sudo source code:



            /* Stash user's shell for use with the -s flag; don't pass to plugin. */
            if ((ud->shell = getenv("SHELL")) == NULL || ud->shell[0] == '') {
            ud->shell = pw->pw_shell[0] ? pw->pw_shell : _PATH_BSHELL;
            }


            If you use -s option, sudo will use your $SHELL instead of _PATH_BSHELL:



            $ cat ./test.sh
            ps | grep "$$" | awk '{ print $4 }'

            $ ./test.sh
            bash

            $ sudo -s ./test.sh
            bash






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 27 '13 at 10:26









            cuonglmcuonglm

            110k27 gold badges224 silver badges326 bronze badges




            110k27 gold badges224 silver badges326 bronze badges











            • 1





              Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

              – Stéphane Chazelas
              Sep 27 '13 at 11:05













            • In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

              – Stéphane Chazelas
              Sep 27 '13 at 12:21














            • 1





              Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

              – Stéphane Chazelas
              Sep 27 '13 at 11:05













            • In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

              – Stéphane Chazelas
              Sep 27 '13 at 12:21








            1




            1





            Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

            – Stéphane Chazelas
            Sep 27 '13 at 11:05







            Note that -s doesn't mean the script will be interpreted by $SHELL, it just means that (assuming you've been authorised to use $SHELL as the target user), the command is started as $SHELL -c ./test.sh. Shells like bash, yash or ksh93 may choose to interpret the she-bang-less scripts with a copy of themselves, others would not and would call the system's sh instead.

            – Stéphane Chazelas
            Sep 27 '13 at 11:05















            In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

            – Stéphane Chazelas
            Sep 27 '13 at 12:21





            In other words, it's not $SHELL instead of _PATH_BSHELL, it's exec($SHELL, "-c", "./test.sh") unconditionally instead of if (exec("./test.sh") == ENOEXEC) exec(_PATH_BSHELL, "./test.sh") (pseudo-code).

            – Stéphane Chazelas
            Sep 27 '13 at 12:21











            4














            The kernel can only run binary executable images. So how do scripts get run? After all, I can type my_script_without_shebang at a shell prompt and I don't get an ENOEXEC error. Script execution is done not by the kernel, but by the shell. The exec code in the shell usually looks something like:



            /* try to run the program */
            execl(program, basename(program), (char *)0);

            /* the exec failed -- maybe it is a shell script without shebang? */
            if (errno == ENOEXEC)
            execl ("/bin/sh", "sh", "-c", program, (char *)0);


            You can verify that with tracing a dummy shell script without shebang:



            cat > /tmp/foo.sh <<EOF
            echo
            EOF

            chmod u+x /tmp/foo.sh

            strace /tmp/foo.sh 2>&1 | grep exec
            execve("/tmp/foo.sh", ["/tmp/foo.sh"], [/* 28 vars */]) = -1 ENOEXEC (Exec format error)


            So then, the execution proceed as Stephane described - default shell is used (in the above code snippet is hard-coded). This nice UNIX FAQ can answer more.






            share|improve this answer






























              4














              The kernel can only run binary executable images. So how do scripts get run? After all, I can type my_script_without_shebang at a shell prompt and I don't get an ENOEXEC error. Script execution is done not by the kernel, but by the shell. The exec code in the shell usually looks something like:



              /* try to run the program */
              execl(program, basename(program), (char *)0);

              /* the exec failed -- maybe it is a shell script without shebang? */
              if (errno == ENOEXEC)
              execl ("/bin/sh", "sh", "-c", program, (char *)0);


              You can verify that with tracing a dummy shell script without shebang:



              cat > /tmp/foo.sh <<EOF
              echo
              EOF

              chmod u+x /tmp/foo.sh

              strace /tmp/foo.sh 2>&1 | grep exec
              execve("/tmp/foo.sh", ["/tmp/foo.sh"], [/* 28 vars */]) = -1 ENOEXEC (Exec format error)


              So then, the execution proceed as Stephane described - default shell is used (in the above code snippet is hard-coded). This nice UNIX FAQ can answer more.






              share|improve this answer




























                4












                4








                4







                The kernel can only run binary executable images. So how do scripts get run? After all, I can type my_script_without_shebang at a shell prompt and I don't get an ENOEXEC error. Script execution is done not by the kernel, but by the shell. The exec code in the shell usually looks something like:



                /* try to run the program */
                execl(program, basename(program), (char *)0);

                /* the exec failed -- maybe it is a shell script without shebang? */
                if (errno == ENOEXEC)
                execl ("/bin/sh", "sh", "-c", program, (char *)0);


                You can verify that with tracing a dummy shell script without shebang:



                cat > /tmp/foo.sh <<EOF
                echo
                EOF

                chmod u+x /tmp/foo.sh

                strace /tmp/foo.sh 2>&1 | grep exec
                execve("/tmp/foo.sh", ["/tmp/foo.sh"], [/* 28 vars */]) = -1 ENOEXEC (Exec format error)


                So then, the execution proceed as Stephane described - default shell is used (in the above code snippet is hard-coded). This nice UNIX FAQ can answer more.






                share|improve this answer













                The kernel can only run binary executable images. So how do scripts get run? After all, I can type my_script_without_shebang at a shell prompt and I don't get an ENOEXEC error. Script execution is done not by the kernel, but by the shell. The exec code in the shell usually looks something like:



                /* try to run the program */
                execl(program, basename(program), (char *)0);

                /* the exec failed -- maybe it is a shell script without shebang? */
                if (errno == ENOEXEC)
                execl ("/bin/sh", "sh", "-c", program, (char *)0);


                You can verify that with tracing a dummy shell script without shebang:



                cat > /tmp/foo.sh <<EOF
                echo
                EOF

                chmod u+x /tmp/foo.sh

                strace /tmp/foo.sh 2>&1 | grep exec
                execve("/tmp/foo.sh", ["/tmp/foo.sh"], [/* 28 vars */]) = -1 ENOEXEC (Exec format error)


                So then, the execution proceed as Stephane described - default shell is used (in the above code snippet is hard-coded). This nice UNIX FAQ can answer more.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 27 '13 at 10:28









                dsmsk80dsmsk80

                2,2939 silver badges14 bronze badges




                2,2939 silver badges14 bronze badges

































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f92622%2fwhich-shell-will-sudo-use-to-execute-a-shell-script-without-the-shebang-line%23new-answer', 'question_page');
                    }
                    );

                    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







                    Popular posts from this blog

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...