According to sudo(8):
Process modelWhen 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?
shwas beforebash... but actually now almost on all linux distrosshis just a symlink tobash– Christopher Díaz Riveros Aug 24 '17 at 05:28bashtodashas the default shell (/bin/sh). Dash is a smaller and faster shell that supports POSIX features but little more. – John1024 Aug 24 '17 at 05:42ls -l /bin/sh, nowadays it's often used as symbolic link, but name is preserved for compatibility reasons – metamorphling Aug 24 '17 at 05:48#!/usr/bin/bashor#!/usr/bin/env bashthen it is run with bash. This is a good practice anyway, not just with sudo. – Johan Myréen Aug 24 '17 at 05:54shis kept for compatibility reasons, but now is almost never really used – Christopher Díaz Riveros Aug 24 '17 at 05:55shnow exists as a bin for compatibility reasons (in most distros) because historically it was before bash or dash or zsh... – Christopher Díaz Riveros Aug 24 '17 at 14:13shis impersonated bybash, it'sbashrunning in POSIX compatibility mode (as if started with--posix), which means different grammar. – Kusalananda Aug 24 '17 at 14:17