Difference between #!/bin/bash and #!/bin/sh

bin/bash v bin/sh in a nutshell.

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.

First published Feb 18, 2014.

We totally get why you have an ad blocker. If you enjoy reading Geekswipe, turning it off for us helps keep the site alive and the science coming.

276 articles

Aeronautical engineer, product builder, developer, science fiction author, and an explorer. I'm the creator and editor of Geekswipe. I love writing about physics, aerospace, astronomy, and technology.

More by Karthikeyan KC

1 comment

  • Avatar
    S.Kovalyov

    Bad idea to use !#/bin/sh if you are shipping scripts with features that will only work with bash. Happens on a daily basis!

Leave a comment

Only used to notify you of replies. Never published.

Related