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)

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:

/usr/local/bin/bash /path/to/script.bashargs...

(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 #!/usr/bin/env bash

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.

  • However bash is not always installed in /usr/local/bin it depends on your packaging system - which is why the env way is useful – Mark Mar 13 at 9:43

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.

  • 1
    I don't think you've actually read my question? I already know this, however this doesn't answer my question at all. Is there something I should do to make this clearer? What I don't know is the possibility of what I asked lol, I understand you're trying to get the easy points, but I did say without altering the file. To avoid these kinds of responses. – RST Jan 16 at 1:37
  • @RST I read your question but it seems like an XY problem and I was trying to steer it back on track. For your own scripts using ‘Bash 4+ functionality’, use env bash, and for scripts you haven't written, leave them alone! If I write a script for distributing to Mac and use /bin/bash, it's because I want to use the system bash, and using that shebang ensures that happens. If I want to use the latest version of bash available, then env bash is superior, but that doesn't mean I should force other scripts I haven't written to use what I think is best. – grg Jan 16 at 10:08
  • 1
    Simply as some users are on Linux and others on Mac, with 99% of the Linux users having latest bash for years and having included the env based approach. Essentially, yes the best case would be to go and change them all, that's not a possibility right now. That's is why I said once again without modifying the file out of interest. I don't necessarily agree with everything you're saying, but as is life hah. – RST Jan 16 at 10:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.