我正在编写一个脚本,它需要一个简单的短格式的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.
Run Code Online (Sandbox Code Playgroud)
这可以减少到我想要的位4.2.10,通过这个:
bash --version | grep "bash" | cut -f 4 -d " " | cut -d "-" -f 1 | cut -d "(" -f 1
Run Code Online (Sandbox Code Playgroud)
然而,如果该消息由于某种原因而稍微改变,则感觉它容易破裂.
有没有更好的方法来做到这一点,这有什么更好的方法?
Man*_*qui 29
如果您在bash shell中运行,$BASH_VERSION则应设置环境变量:
$ echo $BASH_VERSION
4.2.8(1)-release
Run Code Online (Sandbox Code Playgroud)
解析这应该更容易,更可靠.有关shell设置的环境变量列表,请参见手册页.
blu*_*Cat 23
还有一个特殊的数组(BASH_VERSINFO),其中包含单独元素中的每个版本号.
if ((BASH_VERSINFO[0] < 3))
then
echo "Sorry, you need at least bash-3.0 to run this script."
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅http://www.tldp.org/LDP/abs/html/internalvariables.html:
# Bash version info:
for n in 0 1 2 3 4 5
do
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
done
# BASH_VERSINFO[0] = 3 # Major version no.
# BASH_VERSINFO[1] = 00 # Minor version no.
# BASH_VERSINFO[2] = 14 # Patch level.
# BASH_VERSINFO[3] = 1 # Build version.
# BASH_VERSINFO[4] = release # Release status.
# BASH_VERSINFO[5] = i386-redhat-linux-gnu # Architecture
# (same as $MACHTYPE).
Run Code Online (Sandbox Code Playgroud)
kev*_*kev 10
提取第一部分:
$ echo ${BASH_VERSION%%[^0-9.]*}
4.2.10
Run Code Online (Sandbox Code Playgroud)