I run MacOS and iTerm2. I currently have iTerm2 set up to use a different version of bash installed with brew (bash version 4.4, which is not shipped natively with MacOS). I can verify this by running echo $BASH_VERSION
and getting 4.4.23(1)-release
as output.
I have a huge bash script I want to run, but in the header #! /bin/bash
#! /bin/bash
is defined. For some reason when I run the script the native version of bash (version 3.2), which is found at that path, is being used.
I don't really want to change this header because we run the script in production as well as in development environments (the most important is that it works on production servers which run linux). For now, locally, I changed it to /usr/local/bin/bash to make it work but it will generate diffs in my git flow.
Is it possible to add an alias, or similar, in iTerm which translates /bin/bash
to /usr/local/bin/bash
, or a similar trick?
quick & dirty
Why not call the bash direct and your script as parameter?
/usr/local/bin/bash your_special_production_script.sh
You could change the shebang to #! /usr/bin/env bash
#! /usr/bin/env bash
. This shebang should run on other systems too.
The only difference is that env bash
will search your PATH
for bash
rather than using exactly /bin/bash
. Configure your PATH
on the Mac to point to the new bash
version.
To make sure that you end up with the correct version you could add a check at the beginning of your script; something along the lines of
#! /usr/bin/env bash
versionLt() { [ "$(printf %s\\n "$@" | sort -V | head -n1)" = "$1" ]; }
if versionLt "$BASH_VERSION" 4.4; then
echo "Required bash 4.4 or higher but found bash $BASH_VERSION"
exit 2
fi
It is even possible to re-start the script with another interpreter as long as you have the path to that interpreter. Arguments and stdin will be kept.
exec /path/to/new/version/of/bash "$0" "$@"
You could iterate over a list of possible paths and try to find the correct version, but I think using evn bash
and setting PATH
is the way to go.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.