Difference Between Sh and Bash

  1. What Is a Shell?
  2. How to Find the Current Shell
  3. How to Find the Available Shells
  4. the sh Command and Its Use
  5. Bash and Its Use

This tutorial explains what a shell is, knowing the currently used shell, checking the list of all available shells, and the difference between sh and bash.

What Is a Shell?

A shell is a computer program that takes commands. It also interprets commands and passes them to the operating system for processing. It acts as an interface between the user and the operating system, through which the user can interact with the operating system.

Most Linux operating systems come with at least a single shell program. The shell program will be Bash, Dash, or both.

How to Find the Current Shell

To know the shell that’s currently being used on a Linux system, we can read the /etc/passwd file because it stores user account information. Let us run the command below to know the current shell.

The grep command is used to search for a string of characters in a specified file. The command below searches for user fumba in the passwd file located in /etc. When the grep command finds a match, it prints the line with the result.

$ grep fumba /etc/passwd

W can see from the output below that the default shell for the user fumba is bash.

fumba:x:1000:1000:,,,:/home/fumba:/bin/bash

How to Find the Available Shells

All the available shells in Linux systems are listed in the /etc/shells file. We use the cat command below to display the content of the /etc/shells file.

cat /etc/shells

The output below displays the list of all available shells on our system.

# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen

the sh Command and Its Use

sh is a command name for the Bourne shell. It is the standard command language interpreter for Unix-like operating systems. It executes commands from a command line string, the standard input, or a specified file. sh conforms to POSIX standards.

Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. These standards help in developing cross-platform software for multiple operating systems once the standards are followed.

Most systems have the /bin/sh file, but it is a symbolic link and will not invoke sh. In Ubuntu, /bin/sh is a symbolic link to the Dash shell. We can check by running the command below.

$ file -h /bin/sh

The output below shows that /bin/sh is a symbolic link to dash.

/bin/sh: symbolic link to dash

Let us add #!/bin/sh to a script.

#!/bin/sh

printf "sh points to dash!\n"

The script above specifies /bin/sh as the interpreter. However, since /bin/sh points to dash, the dash shell will execute the script as the interpreter.

Bash and Its Use

Bash stands for Bourne Again shell. Like sh, it is a command language processor and a shell. However, bash is a superset of sh. It supports features of sh and provides more extensions and features. Bash is the default shell for Linux operating systems.

In the beginning, Bash conformed to POSIX standards, but it stopped following the POSIX standards over time as more features and extensions were added. However, bash can still be used in POSIX mode by setting the -posix flag, as shown below.

$ bash --posix

Let us write a bash script that conforms to the POSIX standard.

#!/bin/bash

set -o posix
printf "we are using bash shell!\n"

The bash interpreter will execute the script above since it has been specified in the first line, #!/bin/bash.

The set command enables options in the script. In our case, it enables the bash shell to run the script in POSIX mode.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Linux Bash