I'm writing a script which requires the Bash version number in a simple short format.
I'm aware of bash --version
, but this gives a long output:
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.
This could be cut down to the bit I want, 4.2.10
, by this:
bash --version | grep "bash" | cut -f 4 -d " " | cut -d "-" -f 1 | cut -d "(" -f 1
However, this feels like it would be prone to break if that message ever changed slightly for whatever reason.
Is there a better way to do this, and what is this better way?
There are no Booleans in Bash. However, we can define the shell variable having value as 0 (“ False “) or 1 (“ True “) as per our needs.
brew install bash , then. sudo vi /etc/shells and add /usr/local/bin/bash to the list, then. chsh -s /usr/local/bin/bash [your_username] and restart your terminal in order for your changes to take place.
Open your terminal either by using the Ctrl+Alt+T keyboard shortcut or by clicking on the terminal icon. Use the lsb_release -a command to display the Ubuntu version. Your Ubuntu version will be shown in the Description line. As you can see from the output above, I am using Ubuntu 18.04 LTS.
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 9.1. Internal Variables for more information:
# Bash version information: 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).
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