There's couple ways to approach what you want to achieve.
1. Use $BASH_VERSION
It's sufficient to just to see what's in $BASH_VERSION variable. Personally I'd use subshell like so:
$ (read -d "." version trash <<< $BASH_VERSION; echo "$version" )
4
Note that <<< syntax for here-doc is not portable, if you're going to use it with /bin/sh, which is Dash on Ubuntu and might be something else on a different system
Alternative way is via case statement or if statement. Personally, I'd do this:
bash-4.3$ case $BASH_VERSION in 4.*) echo "Can use associative arrays";; ?) echo "can't use associative arrays" ;; esac
Can use associative arrays
Probably for the sake of portability , you probably should check if such variable is even set at all in the first place with something like [ -n $BASH_VERSION ]
This totally can be rewritten as function to be used in a script. Something a long the lines of:
#!/bin/bash
version_above_4(){
# check if $BASH_VERSION is set at all
[ -z $BASH_VERSION ] && return 1
# If it's set, check the version
case $BASH_VERSION in
4.*) return 0 ;;
?) return 1;;
esac
}
if version_above_4
then
echo "Good"
else
echo "No good"
fi
This is not a one-liner, although this is much better. Quality over quantity.
2. Check what's installed
For that you need to filter output of apt-cache policy like so
$ apt-cache policy bash | awk -F '[:.]' '/Installed:/{printf "%s\n",substr($2,2)}'
4
dpkg-query can also come in handy with some filtering via awk.
$ dpkg-query -W bash | awk '{print substr($2,1,1)}'
4
Note that this is not portable, since if there's no dpkg or apt installed on a system ( for instance, RHEL or FreeBSD ), it won't do you any good.
3. Use set -e to exit script if there's an error
One way to get around it is just simply go ahead an use associative arrays and quit when bash cannot use them. set -e line below #!/bin/bash will allow the script to quit if the script can't use associative array.
This will require you to explicitly tell the user: "Hey, you really need bash version 4.3 or above, otherwise the script won't work". Then the responsibility rests with the user, although some might argue that this is not really a good approach to software development.
4. Abandon all hope and write portable, POSIX-compliant scripts
bash scripts aren't portable because its syntax isn't compatible with Bourne shell. If the script that you're writing is going to be used on a range of different systems, not just Ubuntu alone, then abandon all hope, and find ways to use something other than associative arrays. That might include having two arrays or parsing a configuration file. Consider also switching to a different language, Perl or Python, where syntax is at least more portable than bash.
$BASH_VERSIONand$BASH_VERSINFOvariables? – steeldriver May 20 '17 at 00:12--versionand testing the output was the original intention. I've edited the question accordingly. – WinEunuuchs2Unix May 20 '17 at 16:25--versionparameter and return the version number on the first line. If a person encounters a program that doesn't follow those rules they would write custom code. This question is about all the programs I've found that follow the rules, not about those that break them. For example conky usesconky -v | head -n 1 | cut -d" " -f 2to display version number. – WinEunuuchs2Unix May 21 '17 at 11:51sshanddashdon't even know a--versionoption (ssh uses-Vand dash has no option). Vim spreads the version information across multiple lines. Even in the outputs you have shown, the location of the version number varies. – muru May 22 '17 at 08:09--versionparameter"? – WinEunuuchs2Unix May 22 '17 at 17:13