Default Bash 3 overriding shebang /bin/bash with brew installed bash 4

bashcommand linehomebrewscriptterminal

Background

Bash 4 installed successfully using homebrew and the additional steps required to make bash 4 your default shell. Many guides on the net with a simple google: Example Bash 4 upgrade guide for Mac

  • Bash 4.x (installed via brew) location: /usr/local/bin/bash
  • Bash 3.2 (Default OSX) location: /bin/bash

Problem

Scripts I can not control/modify the shebang, as it would affect other users. Unless I was to manually change and then revert before committing to source control. Another option is to type: bash some_script_with_bin_bash_shebang.shto force the use of the newly defaulted bash 4 shell.

I only ever need to do this for Bash 4+ functionality, which is very common now.

Question

Does anyone have a cool technique, workarounds to alias a shebang bash path like so: #!/bin/bash to the newer bash 4 shell at /usr/local/bin/bash?

Without obviously modifying the file! ^_^ (e.g. adding #!/usr/bin/env bash)

Best Answer

This is the main reason nowadays for using a different shebang:

#!/usr/bin/env bash

This will use the first bash in the PATH, which will most likely be the latest version. Since /bin is in the standard path (getconf PATH), this falls back gracefully for those without a custom bash.