Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, February 22, 2016

How to get bash version number

How to get bash version number


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     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?

Thanks

Answer by jogojapan for How to get bash version number


There seems to be an environment variable for this:

echo $BASH_VERSION  

yields

4.1.7(1)-release  

on my machine.

Answer by Mansoor Siddiqui for 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.

Answer by kev for How to get bash version number


To extract the first part:

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

Answer by blueCat for How to get bash version number


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

Answer by mklement0 for How to get bash version number


Building on bluecat's helpful answer:

To report the first 3 version-number components - e.g., 4.2.10 - via built-in array shell variable BASH_VERSINFO:

$ bash -c 'IFS=.; echo "${BASH_VERSINFO[*]: 0:3}"'  4.2.10  

If you're calling this from inside a Bash script, use a subshell in order to localize the effect of changing IFS:

#!/usr/bin/env bash    (IFS=.; echo "${BASH_VERSINFO[*]: 0:3}")  # -> e.g., '4.2.10'  

Explanation:

  • IFS=. sets the internal field separator to ., which ensures that when we print an array inside a double-quoted string later, the array's elements are joined with that separator.

  • ${BASH_VERSINFO[*]: 0:3} extracts the first 3 elements from array variable $BASH_VERSINFO (starting at index 0, return 3 elements).

    • Note that in order for IFS to work as intended, * rather than @ must be used to reference the array; @ would invariably separate the elements with a single space each, irrespective of the value of $IFS.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.