What is the difference between bash and sh?
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Question
00:35 Accepted answer (Score 33)
01:07 Answer 2 (Score 6)
02:14 Answer 3 (Score 4)
04:34 Answer 4 (Score 2)
04:57 Thank you
--
Full question
https://superuser.com/questions/125728/w...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#unix #bash #shell
#avk47
ACCEPTED ANSWER
Score 33
bash
is a superset of sh
ie. everything you can do in sh
you can do in bash
.
Bash has more features (branching, builtins, arrays) making script easier to write. Some later *nix'es have /bin/sh
as a link to /bin/bash
For a full explanation of what here's a tutorial
ANSWER 2
Score 6
Traditionally, /bin/sh would have been the original Bourne shell, which has no history or command-line editing, and no job control.
For about the last 15 years or so, most Unixes have had the POSIX shell installed, or at least ksh or bash (which are very nearly POSIX-like), but still have the more limited shell in /bin/sh
The reason for that is so that older shell scripts which expect the older sh
command will still work.
Since characters like {
, }
and !
have special meaning to bash, it's possible that an older shell script using those characters (without escaping them) could fail.
(The Bourne shell would take !!{1,2}
literally, whereas bash would interpret that as a repeat of the previous command (!!
) followed by a brace-expansion).
On Linux though, the sh
command is almost always just a link to bash
, with all the same features.
ANSWER 3
Score 2
Actually, even though /bin/sh may be a link to /bin/bash, if started up as sh it behaves differently. From the bash manpage:
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.
So as sh
, it tries to emulate historical sh behavior. As bash
, it tries to be as useful as possible as an interactive login shell.
ANSWER 4
Score 2
On many systems and on Solaris in particular, bash is dynamically linked while sh is statically linked. This may pose a security threat, for this reason root user should only use /bin/sh as shell (if you ever need to log as root).