Mac: Virtual Shell Bash Version does not Match Installed Version [duplicate]

这一生的挚爱 提交于 2021-01-20 08:59:12

问题


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?


回答1:


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



回答2:


Your shell script should start with the line:

#!/<path>/bash

Which should point to the installed bash you want. Since you seem to be contend with the bash that was found by executing bash --version, you can execute which bash to tell you where that one is.

Alternately, you can start your script with:

#!/usr/bin/env bash

Which will use the bash that is found on your PATH.



来源:https://stackoverflow.com/questions/51546186/mac-virtual-shell-bash-version-does-not-match-installed-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!