如何在OS X中获取Bash版本号 [英] How to get Bash version number in OS X
问题描述
我正在编写一个需要Bash 4.x的安装脚本.该安装脚本也可以在OSX上使用.我知道在Linux系统上,可以通过使用echo $BASH_VERSION
env变量进行检查来获得Bash版本,但是如何在Darwin中获得Bash版本呢?运行bash --version
将给出:
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_VERSION
env variable but how do I get the bash version in Darwin? Running bash --version
will give:
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 not the output I want. I want the output to be just the version number, specially just the main version number.
推荐答案
echo $BASH_VERSION
在Mac OS X上也适用:
echo $BASH_VERSION
works on Mac OS X as well:
$ echo $BASH_VERSION
3.2.57(1)-release
如果您需要通过调用其路径中的bash
来检查它们是否安装了更新的bash(例如通过Homebrew或MacPorts),则可以从该版本的bash
中执行该命令:
If you need to check if they have a newer bash installed, (such as via Homebrew or MacPorts) by calling the bash
that is in their path, you can just execute that command from within that version of bash
:
$ bash -c 'echo $BASH_VERSION'
4.3.30(1)-release
要仅获取该版本的一个组件,请使用 array ,BASH_VERSINFO
,因此您可以分别访问每个元素.如果您只想要主要版本(这是在我的系统上,我的登录shell是Bash 3,但是我安装了Bash 4进行脚本编写):
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):
$ 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
这篇关于如何在OS X中获取Bash版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!