[英]Is it possible to add an alias to what bash version should be used when #!/bin/bash is specified in a shell script header?
I run MacOS and iTerm2. 我运行MacOS和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).
我目前已将iTerm2设置为使用在brew中安装的不同版本的bash(bash版本4.4,MacOS本身未附带)。 I can verify this by running
echo $BASH_VERSION
and getting 4.4.23(1)-release
as output. 我可以通过运行
echo $BASH_VERSION
并获取4.4.23(1)-release
作为输出来验证这一点。
I have a huge bash script I want to run, but in the header #! /bin/bash
我有一个想要运行的大型bash脚本,但是在标题
#! /bin/bash
#! /bin/bash
is defined. #! /bin/bash
已定义。 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. 由于某些原因,当我运行脚本时,会使用该路径下的bash本机版本(3.2版)。
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). 我真的不想更改此标头,因为我们在生产环境和开发环境中都运行脚本(最重要的是,它可以在运行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.
现在,在本地,我将其更改为/ usr / local / bin / bash使其工作,但是它将在git流中生成差异。
Is it possible to add an alias, or similar, in iTerm which translates /bin/bash
to /usr/local/bin/bash
, or a similar trick? 是否可以在iTerm中添加别名或类似名称,以将
/bin/bash
为/usr/local/bin/bash
或类似的技巧?
quick & dirty 快速又脏
Why not call the bash direct and your script as parameter? 为什么不直接调用bash并将脚本作为参数?
/usr/local/bin/bash your_special_production_script.sh
You could change the shebang to #! /usr/bin/env bash
您可以将shebang更改为
#! /usr/bin/env bash
#! /usr/bin/env bash
. #! /usr/bin/env bash
。 This shebang should run on other systems too. 该shebang也应在其他系统上运行。
The only difference is that env bash
will search your PATH
for bash
rather than using exactly /bin/bash
. 唯一的区别是
env bash
会在您的PATH
搜索bash
而不是完全使用/bin/bash
。 Configure your PATH
on the Mac to point to the new bash
version. 在Mac上配置
PATH
,使其指向新的bash
版本。
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. 您可以遍历可能的路径列表并尝试找到正确的版本,但是我认为使用
evn bash
并设置PATH
是evn bash
的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.