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.
I seem to be able to use this:
and
You can use the following one liner to extract the version number:
echo $BASH_VERSION
works on Mac OS X as well: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 ofbash
: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):You can see the full contents of the array as well: