The standard location of bash is /bin
, and I suspect that's true on all systems. However, what if you don't like that version of bash? For example, I want to use bash 4.2, but the bash on my Mac is at 3.2.5.
I could try reinstalling bash in /bin
but that may be a bad idea. If I update my OS, it will be overwritten.
However, I could install bash in /usr/local/bin/bash
, and setup my PATH to:
PATH="/usr/local/bin:/bin:/usr/bin:$HOME/bin"
Now, if I specify bash
, I don't get the old cruddy one at /bin/bash
, but the newer, shinier one at /usr/local/bin
. Nice!
Except my shell scripts have that !# /bin/bash
shebang. Thus, when I run my shell scripts, I get that old and lousy version of bash that doesn't even have associative arrays.
Using /usr/bin/env bash
will use the version of bash found in my PATH. If I setup my PATH, so that /usr/local/bin/bash
is executed, that's the bash that my scripts will use.
It's rare to see this with bash, but it is a lot more common with Perl and Python:
- Certain Unix/Linux releases which focus on stability are sometimes way behind with the release of these two scripting languages. Not long ago, RHEL's Perl was at 5.8.8 -- an eight year old version of Perl! If someone wanted to use more modern features, you had to install your own version.
- Programs like Perlbrew and Pythonbrew allow you to install multiple versions of these languages. They depend upon scripts that manipulate your PATH to get the version you want. Hard coding the path means I can't run my script under brew.
- It wasn't that long ago (okay, it was long ago) that Perl and Python were not standard packages included in most Unix systems. That meant you didn't know where these two programs were installed. Was it under
/bin
? /usr/bin
? /opt/bin
? Who knows? Using #! /usr/bin/env perl
meant I didn't have to know.
And Now Why You Shouldn't Use #! /usr/bin/env bash
When the path is hardcoded in the shebang, I have to run with that interpreter. Thus, #! /bin/bash
forces me to use the default installed version of bash. Since bash features are very stable (try running a 2.x version of a Python script under Python 3.x) it's very unlikely that my particular BASH script will not work, and since my bash script is probably used by this system and other systems, using a non-standard version of bash may have undesired effects. It is very likely I want to make sure that the stable standard version of bash is used with my shell script. Thus, I probably want to hard code the path in my shebang.
env
may not be located at/usr/bin
. Shebang comments are altogether a bad idea IMHO. If your default script interpreter doesn't handle shebang comments, it is just a comment. However, if you know the script interpreter can handle shebang comments, and you know the path to bash, there is no reason not to invoke it using its absolute path unless the path is too long (unlikely), or you might possibly port the script to a system that doesn't have bash located in /bin. Then again, the caveats I previously mentioned apply in that case since it involves portability./etc
or/bin/sh
.bash
is an add-on for most Unix like systems. It is only Linux wherebash
is guaranteed to be in/bin
and most likely also linked as/bin/sh
. Since Linux became the modern de facto Unix for a lot of people the fact that systems other than Linux might exist has been forgotten. In my own answer below I assumed Linux because you saidbash
. A lot of the BSD boxes I have worked with did not even have it installed./bin/bash
. Bash is not installed by default. If you want it, you have topkg install bash
. Once installed it is located at/usr/local/bin/bash
. There is nothing installed at/bin/bash
on OpenBSD. A shebang of#!/bin/bash
will error, and#!/usr/bin/env bash
will succeed.