Skip to content

Instantly share code, notes, and snippets.

@mdeguzis
Last active April 6, 2021 17:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdeguzis/1d429ae8a8ccff78ea82 to your computer and use it in GitHub Desktop.
Save mdeguzis/1d429ae8a8ccff78ea82 to your computer and use it in GitHub Desktop.
Difference between sh and bash

Source: StackOverflow
By: roman-cheplyaka
Note: Full credit goes to the author and source website you see above.

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 of sh (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 an sh-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 itself bash 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 as sh.

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:

  1. Modern Debian and Ubuntu systems, which symlink sh to dash by default;
  2. Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.
  3. BSDs. OpenBSD uses pdksh, a descendant of the Korn shell. FreeBSD's sh is a descendant of the original UNIX Bourne shell.

Shebang line

Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.

E.g. #!/bin/sh
will use sh (and whatever that happens to point to), #!/bin/bash
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. #!/bin/dash
Which one to use

For my own scripts, I prefer sh for the following reasons:

  • it is standardized
  • it is much simpler and easier to learn
  • it is portable across POSIX systems — even if they happen not to have bash, they are required to have sh.

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. Plain sh is a very minimalistic programming language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment