I'm looking for the best bash one-liner to check if the version number is >= to a specific number. For example I have:
$ bash --version GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2013 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.
In order to use associative arrays the bash version number must be >=4. In my bash script I'd like to put in a one-liner test in the most elegant / efficient / readable way possible.
I anticipate many answers to accomplish the goal so all that work will be gratefully up-voted.
Although this question is specific to bash version number, with modification the answers could apply to other applications such as LibreOffice, Python, systemd, kernel, etc. which might provide different functionality based on version number.
I'm adding an additional example because there can be many programs you may call in your application and some may need to be at a specific version.
Here is a real life example:
$ yad --version 0.37.0 (GTK+ 3.18.9)
YAD (Yet Another Dialog box) released version
0.39 on April 27, 2017 and it hasn't made it's way to Ubuntu repositories yet. To use new features I would have to manually download and compile from source. If I do this the test would be
--version examples from testing (first line displayed only):
$ grep --version grep (GNU grep) 2.25 $ /bin/dd --version dd (coreutils) 8.25 $ /bin/rm --version rm (GNU coreutils) 8.25 $ ls --version ls (GNU coreutils) 8.25
Great answers have been posted for BASH version checking ONLY. I'm hoping for a robust script that works for more than bash. Perhaps "--version" can be universally appended after the program name in the script? Does the version number compare properly? ie. A simple string test "4.9.10 > 4.11.1" probably returns true in bash but it should be false.
Comparing version numbers with decimal separators
There are many ways of comparing two version numbers containing decimal separators as answered in stack overflow. The question and accepted answer were posted in 2010 but, I'm leaning towards the answer posted December 2016.