What is the Difference Between Using bash and sh?

Linux supports different types of shells like “sh(Bourne shell),” “bash(GNU Bourne-Again shell),” “Korn shell,” “Z shell,” and “Korn shell.” Each shell has its unique properties and is different from others based on its specifications. A shell is a command line interpreter acting as a communication medium or interface between the system user and kernel. It executes the commands and receives the output/ response from the kernel. 

This post provides the key differences between using the bash and sh shell.

What is a “bash” Shell?

The “bash” refers to the “Bourne Again Shell”. The “bash” shell is also known as the enhanced version, i.e., the successor of the “sh” shell. In various Linux distributions, it is the default login shell. It supports exclusive features like associative arrays with indices, process substitution, command substitution, and many other variables and special characters.

The user can also name it a programming language because it supports functions, flow control, and variables like other programming languages.

Shebang Line

The first line of the script is the “ shebang line” that tells the given script to run in which shell. In this case, the “bash” script specifies the “bash shebang” written at the start of the script:

#!/bin/bash

The current “Sample.sh” script will execute in the bash shell.

What is a “sh” Shell?

The “sh” shell corresponds to the “Original Bourne Shell.” It is considered as the command line shell program that is designed for UNIX and UNIX-like operating systems. It is implemented by programs known as “shells” like dash, Bourne, Kash, csh, and zsh shell.

Shebang Line

The shebang line for the “Shell” script is as follows:

#!/bin/sh

The “/bin/sh” is symbolically linked to the POSIX-compliant shell “dash” by Debian-based Linux distros. That’s why the  “code.sh” script is executed by a dash.

Difference Between bash and sh Shell

The key differences between “bash” and “sh” shells are listed below:

  • Functionality: The “bash” is the superset of the “sh” shell. It is because the “bash” shell supports the handy features of the Korn and C shells, such as job control, directory manipulation, aliases, and many others.
  • POSIX Shell: The “bash” is not POSIX-compliant, known as the dialect of POSIX language. However, the “sh” follows POSIX standards and is reliable with various Linux operating systems in the form of a kash, dash, and the original Bourne shell.
  • Programming language: The “bash” acts as a programming language because of scripting and implementation. On the other hand, the “sh” shell provides a detailed description of syntax and semantics and can’t be called a traditional programming language.
  • Compatibility: The “bash” is specifically designed for “bash” scripts, while the “sh” is compatible with any shell. That’s why the “bash” is portable as compared to “sh.”
  • Command History: The “bash” supports the commands history that is executed previously via Up-arrow or CTRL-R key. The “sh” does not support this feature like the “bash” shell.
  • Script: The “bash” script only runs on the “bash” shell, whereas the “sh” script can be run on any other shell. This is because the “sh” defines the original shell scripting language.

Which one is Better: “bash” or “sh”?

The use of both “bash” or “sh” shells depends on the user’s choice. Some people prefer the “sh” shell because it is compatible with others operating systems. On the other hand, most people use “bash” due to its extensive features. Aside from it, the “bash” script can be easily executed in the “sh” shell because it is the predecessor of the “bash” shell.

Conclusion

The “bash” is the successor of the “sh” shell, and the “sh” shell is the predecessor of “bash.” Both these shells are different from each other due to their “functionality,” “POSIX shell,” “programming language,” “command history,” “script,” and the “compatibility.” The usage of both these shells relies on the user’s choice. However, the “sh” shell is the original Bourne shell, and its script is compatible with any shell type.

This post has provided the difference between bash and sh shell in Linux.