How to get Bash version number

If you're running within a bash shell, then the $BASH_VERSION environment variable should be set:

$ echo $BASH_VERSION
4.2.8(1)-release

That should be easier and more reliable to parse. See the man page for a list of environment variables set by the shell.


There's also a special array (BASH_VERSINFO) containing each version number in separate elements.

if ((BASH_VERSINFO[0] < 3))
then 
  echo "Sorry, you need at least bash-3.0 to run this script." 
  exit 1 
fi

See http://www.tldp.org/LDP/abs/html/internalvariables.html for more info:

# 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).

To extract the first part:

$ echo ${BASH_VERSION%%[^0-9.]*}
4.2.10

Tags:

Shell

Bash