我正在编写一个需要获取Bash版本号的脚本,希望以简短格式呈现。
我知道可以使用bash --version
命令获取,但是输出很长:
GNU bash, version 4.2.10(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 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.
通过以下方式,可以缩小为我想要的部分 4.2.10
:
bash --version | grep "bash" | cut -f 4 -d " " | cut -d "-" -f 1 | cut -d "(" -f 1
不过,如果那个消息因为任何原因略微更改,这种方法似乎容易出现故障。
有没有更好的方法来解决这个问题,如果有的话,什么是更好的方法?
$BASH_VERSINFO
,通过它我可以仅使用所需的位数来构建它。但是有一点奇怪的是,我机器上的 man 页面没有提到它设定的环境变量。谢谢 :) - stwalkerster