1

I am trying to create shell scripts that make use of Bash 4.0's features. I am on a Mac using zshell as my main shell, and I have bash 4.0 installed via Homebrew.

When I run bash --version from iTerm, I get:

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.

But if I try to execute echo "Using $BASH_VERSION" from within my script, I get: Using version 3.2.57(1)-release

How do I get my virtual shell for my scripts to point to Bash 4.0?

4

2 回答 2

3

homebrew makes symbolic links in /usr/local/bin for everything it installs, so if you want to use homebrew-provided bash, use:

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

If you want to see the symbolic link and where it is pointing:

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

If you want information about homebrew bash, use:

brew info bash

If you want to use it as your login shell (I don't like the idea personally), you need to add it to /etc/shells and use chsh command.

$ echo /usr/local/bin/bash | sudo tee -a /etc/shells
$ chsh -s /usr/local/bin/bash

then open a new terminal window and verify that it works by executing

$ echo $BASH_VERSION
于 2018-07-26T19:57:13.340 回答
1

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

#!/<path>/bash

哪个应该指向您想要的已安装 bash。由于您似乎与通过执行找到的 bash 抗衡bash --version,您可以执行which bash以告诉您那个在哪里。

或者,您可以使用以下命令开始您的脚本:

#!/usr/bin/env bash

它将使用在您的 PATH 中找到的 bash。

于 2018-07-26T19:35:09.627 回答