Firstly, you could consider simply replacing
/bin/bash with a more uptodate version.
Secondly, if you're not comfortable
voiding your warranty doing that, and where you already have a problem with an existing script, bypass its
#! line by invoking it as:
(You can just use
bash rather than
/usr/local/bin/bash if your
$PATH is in the right order.)
Thirdly, if this script is specific to Apple devices and must have Bash version 4, then always set it explicitly to
#!/usr/local/bin/bash, as there's no benefit to setting it to anything else.
Fourthly, consider using https://gist.github.com/kurahaupo/8130030 - note that you need the "source" version of the script to not be in
/usr/local/bin; directly in your source repo would be ideal.
Source vs Deploy
Your query about setting the
#! line in the source repo highlights an extremely prevalent misapprehension about scripts: the idea that a script, simply because it is "text", should not need to be "prepared for use" in any sense.
For other types of programs it's obvious: they have to be compiled. And if they're large python or perl scripts, they need a package or installer that will include any module dependencies.
But even a shell script needs, at minimum, to have its ownership and permissions set, and to be placed into a
bin directory. Typically this would be either when a package is built for a class of device, or when the script is installed on a target device. This is also the appropriate time to set the
#! line to match the deployment environment.
I do not recommend
The widespread promotion of
#!/usr/bin/env slightly weakens overall security, but in a way that's not likely to bite the people who write it, only the users somewhere down the track. Therefore I do not recommend it; indeed I predict that eventually it's going to come back and bite your clients, or your clients' clients, or your clients' clients' ... clients.
It's promoted as "portable", but that's not actually true: Android and OpenWRT devices do not even have
/usr much less
/usr/bin/env, so it's quite useless there. So in practice the main beneficiaries are Apple users.
If the script requires a new version of bash, then the system version simply won't do, so it's pointless using it as a fallback. If you don't need the latest version, then having it fixed as /bin/bash is fine.
In short, if you're deploying a script to Apple that requires Bash version 4, then you should use
#!/usr/local/bin/bash, either when you build the script into an installable package, or when the script is installed on a particular device.