According to sudo(8):
Process model
When sudo runs a command, it calls fork(2), sets up the execution environment as described above, and calls the execve system call in the child process.
Also, I have found that the forked child process execs its command using sh
.
So if the command is a bash script with some bash-specific command such as source
in it, the sh
will not exec it correctly. For example:
% cat /tmp/wibble source something % ls -l /tmp/wibble -rwxr-xr-x 1 user user 17 Aug 24 08:32 /tmp/wibble % getent passwd root root:x:0:0:root:/root:/bin/bash % /tmp/wibble /tmp/wibble: 1: /tmp/wibble: source: not found % /bin/bash /tmp/wibble ~ [pts/3.4028.1] /tmp/wibble: line 1: something: No such file or directory % /bin/dash /tmp/wibble /tmp/wibble: 1: /tmp/wibble: source: not found % /bin/sh /tmp/wibble /tmp/wibble: 1: /tmp/wibble: source: not found % echo $SHELL /bin/zsh % sudo /tmp/wibble /tmp/wibble: 1: /tmp/wibble: source: not found % sudo -s /tmp/wibble /tmp/wibble: 1: /tmp/wibble: source: not found % sudo -i /tmp/wibble /tmp/wibble: line 1: something: No such file or directory % export SHELL=/bin/bash % sudo /tmp/wibble /tmp/wibble: 1: /tmp/wibble: source: not found % sudo -s /tmp/wibble /tmp/wibble: line 1: something: No such file or directory % sudo -i /tmp/wibble /tmp/wibble: line 1: something: No such file or directory %
Often we can append a -s
option to sudo
to solve this problem, as in the aforegiven example, but I'd like to know why sudo
uses sh
as default. Is it so that it can be configured to other shells?
sh
was beforebash
... but actually now almost on all linux distrossh
is just a symlink tobash
bash
todash
as the default shell (/bin/sh
). Dash is a smaller and faster shell that supports POSIX features but little more.ls -l /bin/sh
, nowadays it's often used as symbolic link, but name is preserved for compatibility reasonssh
is impersonated bybash
, it'sbash
running in POSIX compatibility mode (as if started with--posix
), which means different grammar.