Difference between #!/bin/bash and #!/bin/sh
When configuring shell scripts, you would have come across directives like #!/bin/sh
. This directive (also known as a shebang) is used to tell your *nix OS that the file is to be treated as an executable. In other words, treat any file with #!/bin/sh
as a script and spawn the interpreter as mentioned.
A very common example of this is a python script file, where you might have used #!/usr/bin/python3
. This spawns a Python interpreter for the scripts that use this shebang.
And chances are, you would’ve also noticed a few scripts with #!/bin/bash
as well. How is bash
different from sh
? What about dash
?
#!/bin/sh vs #!/bin/bash
Both sh
and bash
are *nix shells. On Linux, there are multiple shells available and the most popular one is bash
. The sh
shebang would mostly be seen in older scripts that used the Bourne shell.
As most Linux OS use bash
as their default shell these days, the shebang #!/bin/sh
is set as a symbolic link to #!/bin/bash
these days. In fact, the name ‘bash’ stands for ‘Bourne-Again Shell’, which is widely accepted as a replacement to the ‘Bourne Shell’ with better features.
Which shell is better to use?
It depends. If you are using features that are only available on bash
you are better with !#/bin/bash
. If you are distributing your shell scripts publicly, you can use !#/bin/sh
as in most modern *nix operating systems, this will be a symbolic link to the default system shell. In Ubuntu, it defaults to bash.
This post was first published on February 18, 2014.
Bad idea to use
!#/bin/sh
if you are shipping scripts with features that will only work with bash. Happens on a daily basis!