Using env

Another problem with porting Bash scripts is that Bash is not always reliably in the same location. On GNU/Linux systems, it's almost always available in /bin/bash. On other systems, such as FreeBSD, it might instead be in /usr/local/bin/bash.

One way of working around this portability problem is to use a trick with the /usr/bin/env program; using it as a shebang with bash as the argument will find the first bash program in PATH:

#!/usr/bin/env bash

This works well on most systems, but it's not perfect. One subtle problem with it is that it means the bash executable to run the script is chosen at runtime. This means that a user with their own copy of bash installed or with a PATH that points to a different version of bash  might ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.