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
works on Mac OS X as well:
$ 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 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
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
You can use the following one liner to extract the version number:
bash --version | awk 'NR==1{print $4}'
user@ubuntu-server:~$ bash --version |awk 'NR==1{print $4}'
4.3.11(1)-release
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