Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

#!/bin/bash
#!/usr/bin/env bash
share|improve this question
1  
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
add comment

1 Answer 1

up vote 9 down vote accepted

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.

share|improve this answer
    
Thank you for the explanation. –  Danijel J Oct 5 '13 at 2:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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