如何在 OS X 中获取 Bash 版本号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29060076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:32:16  来源:igfitidea点击:

How to get Bash version number in OS X

macosbashshell

提问by Sindhu S

I am writing a install script which needs Bash 4.x. This install script can be used on OSX too. I am aware that on Linux systems I can get Bash version by checking with echo $BASH_VERSIONenv variable but how do I get the bash version in Darwin? Running bash --versionwill give:

我正在编写一个需要 Bash 4.x 的安装脚本。这个安装脚本也可以在 OSX 上使用。我知道在 Linux 系统上我可以通过检查echo $BASH_VERSIONenv 变量来获得 Bash 版本,但是如何在 Darwin 中获得 bash 版本?运行bash --version将给出:

GNU bash, version 4.3.33(1)-release (x86_64-apple-darwin14.1.0)
Copyright (C) 2013 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.

This is notthe output I want. I want the output to be just the version number, specially just the main version number.

不是我想要的输出。我希望输出只是版本号,特别是主版本号。

回答by Brian Campbell

echo $BASH_VERSIONworks on Mac OS X as well:

echo $BASH_VERSION也适用于 Mac OS X:

$ echo $BASH_VERSION
3.2.57(1)-release

If you need to check if they have a newer bash installed, (such as via Homebrew or MacPorts) by calling the bashthat is in their path, you can just execute that command from within that version of bash:

如果您需要通过调用bash路径中的来检查他们是否安装了较新的 bash(例如通过 Homebrew 或 MacPorts),您可以从该版本中执行该命令bash

$ bash -c 'echo $BASH_VERSION'
4.3.30(1)-release

To get just one component of the version, there is an array, BASH_VERSINFO, so you can access each element individually. If you just want the major version (this is on my system, where my login shell is Bash 3 but I have Bash 4 installed for scripting):

为了仅获取版本的一个组件,有一个数组, BASH_VERSINFO,因此您可以单独访问每个元素。如果您只想要主要版本(这是在我的系统上,我的登录 shell 是 Bash 3,但我安装了 Bash 4 用于编写脚本):

$ echo ${BASH_VERSINFO[0]}
3
$ bash -c 'echo ${BASH_VERSINFO[0]}'
4

You can see the full contents of the array as well:

您还可以看到数组的完整内容:

$ echo "${BASH_VERSINFO[@]}"
3 2 57 1 release x86_64-apple-darwin14
$ bash -c 'echo "${BASH_VERSINFO[@]}"'
4 3 30 1 release x86_64-apple-darwin14.0.0

回答by tcsapunaru

You can use the following one liner to extract the version number:

您可以使用以下一行来提取版本号:

bash --version | awk 'NR==1{print }'

user@ubuntu-server:~$ bash --version |awk 'NR==1{print $4}'

4.3.11(1)-release

user@ubuntu-server:~$ bash --version |awk 'NR==1{print $4}'

4.3.11(1)-发布

回答by Mark Setchell

I seem to be able to use this:

我似乎可以使用这个:

echo ${BASH_VERSINFO[0]}
3
echo ${BASH_VERSINFO[1]}
2
echo ${BASH_VERSINFO[2]}
57

and

echo $BASH_VERSION
3.2.57(1)-release