Bash compare version numbers

Here is a little script to help compare version numbers in the format X.X.X using bash/shell. This can be good if you are trying to find out which version is higher for something like an upgrade.

versionlte() {
    [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
versionlt() {
    [ "$1" = "$2" ] && return 1 || versionlte $1 $2
}

You can then use this as an iffe.

lowerVersion="1.0.2"
higherVersion="2.3.1"

versionlt $lowerVersion $higherVersion && upgradeRequired=true || upgradeRequired=false

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.