When writing shell scripts in Linux, the first line that you need to write is a shebang line, which specifies the interpreter to use for executing the script. The shebang line typically starts with #! and then a specific interpreter will be used that a user wants to access.
Two commonly used interpreters are /bin/sh and /bin/bash. While both of these interpreters are compatible with the Bourne shell syntax, there are some important differences between them that can affect the behavior of your shell scripts.
Although /bin/sh and /bin/bash both belong to the Unix shell, they have some key differences, making them unique. This post will elaborate on what is “#!/bin/sh” and “#!/bin/bash”, alongside the notable differences among these.
What is #!/bin/sh?
The shebang line #!/bin/sh tells the interpreter to use the shell script while executing a code. In this case, /bin/sh refers to the Bourne shell, which is a simple and lightweight shell that is available on most Unix-based systems, including Linux.
Scripts that use #!/bin/sh are typically written in the Bourne shell syntax, which is a subset of the POSIX shell standard. Since /bin/sh is a lightweight shell, scripts that use this interpreter tend to be more portable across different Unix-based systems.
How to Use #! /bin/sh in Linux?
You can use #!/bin/sh by opening a bash script file, and then at the top, write this line as shown below:
What is #!/bin/bash?
The #!/bin/bash is a shebang line in Linux that indicates to use of a bash shell-based interpreter for shell script execution. Bash is a more advanced version of the older Bourne shell, which is also known as the “Bourne-Again SHell”.
Those features include command line editing, history, and programmable completion. When a script is executed with #!/bin/bash shebang line, the script is interpreted by the Bash shell, which then executes the commands specified in the script.
How to Use #!/bin/bash in Linux?
You need to write #!/bin/bash at the top of a bash script, as shown below:
Key Differences Between /bin/sh and /bin/bash
The key differences between these two will be discussed in this section.
Features: One of the biggest differences between these shell scripts is that the bash shell has more features and functionalities than the .sh shell. Those features include command line completion, history, and job control. Additionally, bash script comes with arrays, variables, and functions, making it easier to write complex scripts.
Performance: Sh and bash shell performance might vary substantially depending on the job. The “.sh” is quicker than bash for simple tasks, while bash is better for more complicated activities, such as running huge scripts.
Syntax: The main syntax of sh and bash are almost identical, but several important variations between the shells might cause compatibility issues. For example, for conditional commands, bash uses square brackets, but sh uses round brackets. Also, bash enables the usage of double square brackets, which is not allowed in a sh script.
The /bin/sh and /bin/bash are both Unix shell scripts but offer different purposes that make them unique. Bash includes many advanced features that are lacking in Bourne shell, such as advanced history management, tab completion, and process substitution. On the other hand, the Bourne shell is faster and more efficient than Bash since it has a smaller code base and fewer features to manage. A detailed comparison between these two has been discussed in this article.