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.
Check Bash version from within shell script #!/bin/bash echo "Checking for Bash version...." echo "The Bash version is $BASH_VERSION !" Once ready, make the file executable and run the script: $ chmod +x check-bash-version.sh $ ./check-bash-version.sh Checking for Bash version.... The Bash version is 4.4.
This version of Bash is included in all versions of macOS, even the newest one. The reason that Apple includes such an old version of Bash in its operating system has to do with licensing. Since version 4.0 (successor of 3.2), Bash uses the GNU General Public License v3 (GPLv3), which Apple does not (want to) support.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With