A typical bash script will identify where its interpreter can be found...
#!/bin/bash
But this doesn't always work because bash might be installed in a different location.
For example, macOS has an old version of bash (or in more modern releases it has swapped bash completely for zsh) and so a user might manually install an updated version of bash which will go into a different directory...
#!/usr/local/bin/bash
To make your scripts portable across different systems you should reference the env
executable and pass it the command you want it to locate (i.e. bash
in this case)...
#!/usr/bin/env bash
The env
executable will use whatever bash executable appears first in the running user's $PATH
variable.
If you have two version of bash installed (bash1 in /bin
and bash2 in /usr/local/bin
) and your PATH was set like so: /home/foo/bin:/usr/local/bin:/usr/bin:/bin
than bash2 would execute the script.
You can use either type env
or command -V env
to locate where the env
command is installed.