I notice that with bash scripts, some people use a different shebang to the one that I'm used to putting at the top of my own.

Can someone simplify the difference between these two? I use the #!/bin/bash one all the time.

#!/usr/bin/env bash
  • 2
    see unix.stackexchange.com/questions/29608/… and take note of what it says about passing arguments to the interpreter. IMO using /usr/bin/env like this is an abuse of the env program perpetrated mostly by the python community because they can't manage interpreter and library versioning properly. – cas Oct 5 '13 at 5:58

The #!/usr/bin/env bash results in the script using whatever bash is found first in $PATH.

While it is common for bash to be located at /bin/bash. There are cases where it is not (different operating systems). Another potential use is when there are multiple bash shells installed (newer version at an alternate location like /usr/local/bin/bash).

Doing #!/usr/bin/env bash just takes advantage of a behavior of the env utility.
The env utility is normally used for manipulating the environment when calling a program (for example; env -i someprog to wipe the environment clean). However by providing no arguments other than the program to execute, it results in executing the specified program as found in $PATH.

Note that there are both advantages and disadvantages to doing this.

The advantages are as mentioned earlier, in that it makes the script portable if bash is installed in a different location, or if /bin/bash is too old to support things the script is trying to do.

The disadvantage is that you can get unpredictable behavior. Since you're at the mercy of the user's $PATH, it can result in the script being run with a version of bash that has different behavior than what the script expects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.