When writing shell programs, we often use /bin/sh
and /bin/bash
. I usually use bash
, but I don't know what's the difference between them.
What's main difference between bash
and sh
?
What do we need to be aware of when programming in bash
and sh
?
Other answers generally pointed out the difference between Bash and a POSIX shell standard. However, when writing portable shell scripts and being used to Bash syntax, a list of typical bashisms and corresponding pure POSIX solutions is very handy. Such list has been compiled when Ubuntu switched from Bash to Dash as default system shell and can be found here: https://wiki.ubuntu.com/DashAsBinSh
Moreover, there is a great tool called checkbashisms that checks for bashisms in your script and comes handy when you want to make sure that your script is portable.
What is sh
sh
(or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88
,dash
, ...).bash
can also be considered an implementation ofsh
(see below).Because
sh
is a specification, not an implementation,/bin/sh
is a symlink (or a hard link) to an actual implementation on most POSIX systems.What is bash
bash
started as ansh
-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions. Many of these extensions may change the behavior of valid POSIX shell scripts, so by itselfbash
is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.bash
supports a--posix
switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked assh
.sh = bash?
For a long time,
/bin/sh
used to point to/bin/bash
on most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently.Some popular examples of systems where
/bin/sh
does not point to/bin/bash
(and on some of which/bin/bash
may not even exist) are:sh
todash
by default;initramfs
. It uses theash
shell implementation.pdksh
, a descendant of the Korn shell. FreeBSD'ssh
is a descendant of the original UNIX Bourne shell. Solaris has its ownsh
which for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.How can you find out what
/bin/sh
points to on your system?The complication is that
/bin/sh
could be a symbolic link or a hard link. If it's a symbolic link, a portable way to resolve it is:If it's a hard link, try
In fact, the
-L
flag covers both symlinks and hardlinks, but the disadvantage of this method is that it is not portable — POSIX does not requirefind
to support the-samefile
option, although both GNU find and FreeBSD find support it.Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
E.g.
will use
sh
(and whatever that happens to point to),will use
/bin/bash
if it's available (and fail with an error message if it's not). Of course, you can also specify another implementation, e.g.Which one to use
For my own scripts, I prefer
sh
for the following reasons:bash
, they are required to havesh
There are advantages to using
bash
as well. Its features make programming more convenient and similar to programming in other modern programming languages. These include things like scoped local variables and arrays. Plainsh
is a very minimalistic programming language.Linux operating system offers different types of shell. Though shells have many commands in common, each type has unique features. Let’s study different kind of mostly used shells.
Sh shell:
Sh shell is also known as Bourne Shell. Sh shell is the first shell developed for Unix computers by Stephen Bourne at AT&T's Bell Labs in 1977. It include many scripting tools.
Bash shell :
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most linux distribution and substitute for Sh Shell (Sh shell will also run in the Bash shell) . Bash Shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.