简体   繁体   English

在shell脚本头中指定#!/ bin / bash时,是否可以为应该使用的bash版本添加别名?

[英]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并设置PATHevn bash的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以确定所谓的 bash shell 脚本? - Is it possible to determine what called a bash shell script? Shell Script 中带有 Shebang (#!/bin/bash) 的“-xe”代表什么? - What does "-xe" with Shebang (#!/bin/bash) in Shell Script stand for? Bash别名为Python脚本 - 是否可能? - Bash alias to Python script — is it possible? git脚本在shell = / bin / sh时起作用,而在shell = / bin / bash时不起作用。 怎么修 - git script works when shell = /bin/sh but not when shell = /bin/bash. How to fix 即使SHELL是/ bin / bash,BASH_VERSION也为空 - BASH_VERSION is empty even though SHELL is /bin/bash 为什么直接执行/ shell / bin / sh或/ usr / bin / bash时,shell脚本不能运行? - Why shell script won't run when executed directly, but runs with /usr/bin/sh or /usr/bin/bash? shell脚本(bash)中的$ {}命令是什么 - what is ${ } command in shell script(bash) MacOS .bash_profile-/ usr / bin / alias:第4行:Builtin:Alias:不是Shell Builtin-如何创建别名? - MacOS .bash_profile - /usr/bin/alias: line 4: builtin: alias: not a shell builtin - how to create an alias? $ {MYPATH //://:// bin:} / bin在bash shell中是什么意思? - what does ${MYPATH//://bin:}/bin mean in bash shell? bash 中使用的 -ex 选项有什么作用? #!/bin/bash -ex 意思是 - What does -ex option used in bash | #!/bin/bash -ex mean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM