Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Bash version number

Tags:

bash

shell

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?

like image 602
stwalkerster Avatar asked Feb 26 '12 04:02

stwalkerster


People also ask

Is it true 1 or 0 bash?

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.

How do I change bash version in Linux?

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.

How do I know my shell version Ubuntu?

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.


2 Answers

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.

like image 112
Mansoor Siddiqui Avatar answered Oct 02 '22 06:10

Mansoor Siddiqui


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). 
like image 27
FelixEnescu Avatar answered Oct 02 '22 05:10

FelixEnescu