Mac:Virtual Shell Bash版本与安装版本不匹配

时间:2018-07-26 19:24:11

标签: bash shell bash4

我正在尝试创建利用Bash 4.0功能的Shell脚本。我在Mac上使用zshell作为主外壳,并且已经通过Homebrew安装了bash 4.0。

从iTerm运行bash --version时,我得到:

GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

但是,如果我尝试从脚本中执行echo "Using $BASH_VERSION",则会得到:Using version 3.2.57(1)-release

如何为脚本获取虚拟外壳程序以指向Bash 4.0?

2 个答案:

答案 0 :(得分:1)

您的shell脚本应以以下行开头:

#!/<path>/bash

应指向所需的已安装bash。由于您似乎不喜欢执行bash --version所发现的重击,因此可以执行which bash来告诉您那是什么。

或者,您可以使用以下命令启动脚本:

#!/usr/bin/env bash

将使用您在PATH上找到的bash。

答案 1 :(得分:1)

自制软件会在/usr/local/bin中为其安装的所有内容建立符号链接,因此,如果要使用 homebrew 提供的bash,请使用:< / p>

#!/usr/local/bin/bash
...
...

如果要查看符号链接及其指向的位置:

ls -l /usr/local/bin/bash

lrwxr-xr-x  1 mark  admin  30 Mar 20 17:16 /usr/local/bin/bash -> ../Cellar/bash/4.4.19/bin/bash

如果您要获取有关自制 bash的信息,请使用:

brew info bash

如果要使用它作为登录外壳(我个人不喜欢这个想法),则需要将其添加到/etc/shells并使用chsh命令。

相关问题