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

编程入门 行业动态 更新时间:2024-10-09 10:21:29
本文介绍了如何在OS X中获取Bash版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个需要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 <gnu/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版本号

本文发布于:2023-11-12 02:16:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:版本号   如何在   OS   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!