On most but not all systems, I recommend using:
#!/bin/bash
It's not 100% portable (some systems place bash
in a location other than /bin
), but the fact that a lot of existing scripts use #!/bin/bash
pressures various operating systems to make /bin/bash
at least a symlink to the main location.
The alternative of:
#!/usr/bin/env bash
has been suggested -- but there's no guarantee that the env
command is in /usr/bin
(and I've used systems where it isn't). Furthermore, this form will use the first instance of bash
in the current user's $PATH
, which might not be a suitable version of the Bash shell.
(But /usr/bin/env
should work on any reasonably modern system, either because env
is in /usr/bin
or because the system does something to make it work. The system I referred to above was SunOS 4, which I probably haven't used in about 25 years.)
If you need a script to run on a system that doesn't have /bin/bash
, you can modify the script to point to the correct location (that's admittedly inconvenient).
I've discussed the tradeoffs in greater depth in my answer to this question.
A somewhat obscure update: One system I use, Termux, a desktop-Linux-like layer that runs under Android, doesn't have /bin/bash
(bash
is /data/data/com.termux/files/usr/bin/bash
) -- but it has special handling to support #!/bin/bash
.
UPDATE: As Edward L. points out in a comment, bash is not part of the "base OS" on FreeBSD, and even if it's installed by default, it probably won't be installed as /bin/bash
. On such a system, you can either use the #!/usr/bin/env
trick (I'm assuming that FreeBSD installed env
as /usr/bin/env
), or you can use the path where bash is installed (apparently that's #!/usr/local/bin/bash
). If your scripts are only intended to run under FreeBSD, you can use #!/usr/local/bin/bash
. If they're meant to be portable, you can use the #!/usr/bin/env
trick (which has some disadvantages; see my answer on cited above) or you can update the #!
line when you install your scripts.
There may well be similar issues on some other operating systems.