Linux Commands

Compare sh Shell vs. Bash

Any modern UNIX/Linux operating system comes with the sh and the Bash shells installed by default. While both of them are very similar in nature, one offers more than the other.

This guide will dive deep into the differences and similarities between the sh and the Bash shells.

Linux Shell

In Linux, a shell is a dedicated program for command interpretation. It interprets the command language and tells the system to perform the tasks described. It’s not a part of the kernel but can perform tasks, such as running programs and creating files.

The functionality of a shell is better understood with an example. Open the terminal in your system and run the following command:

$ date

Here, we noted the following:

  • The default shell took the command “date”, interpreted, and ran the date tool.
  • It also printed the output on the console screen.

Shells can also come with their own languages. Bash scripting, for example, is potent in automating very complex tasks and achieving wonders.

There are lots of shells available for the Linux platform. Obviously, the most popular is the Bash shell. Here are some alternative shells worth checking out:

The Bourne Shell (sh)

The Bourne shell is the default shell for version 7 UNIX. Originally developed by Stephen Bourne at Bell Labs, it was a replacement for the Thompson shell. The Bourne shell started its journey in 1979. Learn more about the history of the Bourne shell on Wikipedia.

Even to this day, the Bourne shell is available on almost all Linux systems. The location of the binary is as follows:

$ which sh

UNIX-like systems continue to have Bourne shell at /usr/bin/sh. It may not be the Bourne shell but a symbolic/hard link to a more compatible shell. You can verify it by running the following command:

$ file -h /bin/sh

The sh shell is also the name of the command programming language. This language is described by POSIX standard that all UNIX and UNIX-like systems must follow. The Bourne shell we’ve discussed so far is one implementation of sh.

The Bourne Again Shell (Bash)

Now, we’ve come to the modern shell we all know and love — the Bash shell, also known as the “Bourne Again Shell.” The naming is a pun indicating that it replaces the Bourne shell.

Developed by Brian Fox for the GNU Project, Bash is both a UNIX shell and a command language. It was first released in 1989. Since then, it’s been the default login shell for most Linux distros. Learn more about the history of the Bash shell on Wikipedia.

Bash is a superset of sh, meaning it incorporates features of sh and some more. As a language, most commands do the same thing as sh. However, Bash isn’t a POSIX-compliant shell but rather, a dialect of the POSIX shell language. Bash is intended to be the conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1).

Check the location of the Bash shell binary:

$ which bash

Differences Between sh and Bash

Provided the historical context, sh and Bash share more similarities than differences as one is a better implementation of the other. Regardless, here are some noticeable feature differences that one should know.

Default Shell

In most of today’s modern systems, Bash is the default shell.

Binary Location

Check the binary location of both shells:

$ which sh
$ which bash

Feature

Compared to sh, Bash offers far more flexibility and syntax that look like a modern programming language. Here are some additional features that Bash offers over sh:

  • Bash supports command-line completion with the TAB key
  • Browse through command history by using the UP arrow key or “Ctrl + R”
  • Arithmetic calculation without any third-party tools
  • Associative arrays
  • Keyboard shortcuts
  • Customization support for the default Bash presentation
  • EPOCHSECONDS and EPOCHREALTIME environment variables
  • Brace expansions

POSIX Compliance

By default, Bash isn’t POSIX compliant, whereas sh is. However, we can run Bash in POSIX compliant mode with the following command:

$ bash --posix

If you’re writing a Bash script but need the POSIX standard, use the following code at the beginning:

#!/bin/bash
set -o posix

Here, the set command tells Bash to enable the POSIX mode.

Ease of Use

Bash offers a more modern command-line language compared to sh. In this regard, you’ll have a more comfortable time using Bash.

Portability

Compared to Bash, sh offers better portability.

Because sh is POSIX compliant, any shell that supports POSIX will be able to run sh scripts. For example, Bash can run sh scripts in POSIX mode.

Scripting

When you’re writing a script in Bash, the code is only guaranteed to be compatible with Bash.

On the other hand, when scripting in sh, the code can be run on any shell. It’s because sh defines the original shell scripting language.

Conclusion

In this article, we explored the history, similarities, and differences between sh and Bash. Sh is the predecessor of Bash. Both of them are available on all the modern UNIX/Linux systems. While Bash offers a more comfortable and easy-to-use experience, sh offers compatibility, portability, and standardized syntax/behavior.

Are you new to Bash scripting? This Bash scripting tutorial for beginners covers everything necessary to write your Bash scripts. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.