How to compare two strings in dot separated version format in Bash?

bash compare two version numbers
bash check version of program
bash compare strings
compare two variable in shell script
bash compare string to integer
bash compare two ints
compare two values in unix shell script
dpkg --compare-versions

Is there any way to compare such strings on bash, e.g.: 2.4.5 and 2.8 and 2.4.5.1?

Here is a pure Bash version that doesn't require any external utilities:

#!/bin/bash
vercomp () {
    if [[ $1 == $2 ]]
    then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            return 2
        fi
    done
    return 0
}

testvercomp () {
    vercomp $1 $2
    case $? in
        0) op='=';;
        1) op='>';;
        2) op='<';;
    esac
    if [[ $op != $3 ]]
    then
        echo "FAIL: Expected '$3', Actual '$op', Arg1 '$1', Arg2 '$2'"
    else
        echo "Pass: '$1 $op $2'"
    fi
}

# Run tests
# argument table format:
# testarg1   testarg2     expected_relationship
echo "The following tests should pass"
while read -r test
do
    testvercomp $test
done << EOF
1            1            =
2.1          2.2          <
3.0.4.10     3.0.4.2      >
4.08         4.08.01      <
3.2.1.9.8144 3.2          >
3.2          3.2.1.9.8144 <
1.2          2.1          <
2.1          1.2          >
5.6.7        5.6.7        =
1.01.1       1.1.1        =
1.1.1        1.01.1       =
1            1.0          =
1.0          1            =
1.0.2.0      1.0.2        =
1..0         1.0          =
1.0          1..0         =
EOF

echo "The following test should fail (test the tester)"
testvercomp 1 1 '>'

Run the tests:

$ . ./vercomp
The following tests should pass
Pass: '1 = 1'
Pass: '2.1 < 2.2'
Pass: '3.0.4.10 > 3.0.4.2'
Pass: '4.08 < 4.08.01'
Pass: '3.2.1.9.8144 > 3.2'
Pass: '3.2 < 3.2.1.9.8144'
Pass: '1.2 < 2.1'
Pass: '2.1 > 1.2'
Pass: '5.6.7 = 5.6.7'
Pass: '1.01.1 = 1.1.1'
Pass: '1.1.1 = 1.01.1'
Pass: '1 = 1.0'
Pass: '1.0 = 1'
Pass: '1.0.2.0 = 1.0.2'
Pass: '1..0 = 1.0'
Pass: '1.0 = 1..0'
The following test should fail (test the tester)
FAIL: Expected '>', Actual '=', Arg1 '1', Arg2 '1'

How to compare two strings in dot separated version format in Bash, How to compare two strings in dot separated version format in Bash - Bash has quietly made scripting on Unix systems a lot easier with its own regular  When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [ [ command for pattern matching.

If you have coreutils-7 (in Ubuntu Karmic but not Jaunty) then your sort command should have a -V option (version sort) which you could use to do the comparison:

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

verlt() {
    [ "$1" = "$2" ] && return 1 || verlte $1 $2
}

verlte 2.5.7 2.5.6 && echo "yes" || echo "no" # no
verlt 2.4.10 2.4.9 && echo "yes" || echo "no" # no
verlt 2.4.8 2.4.10 && echo "yes" || echo "no" # yes
verlte 2.5.6 2.5.6 && echo "yes" || echo "no" # yes
verlt 2.5.6 2.5.6 && echo "yes" || echo "no" # no

How to compare a program's version in a shell script?, I don't know if it is beautiful, but it is working for every version format I know. #!/bin​/bash currentver="$(gcc -dumpversion)" requiredver="5.0.0" if [ "$(printf '%s\n'  Built-in functions are used in many programming language to test the equality of two strings. You can check the equality and inequality of two strings in bash by using if statement. “ == ” is used to check equality and “ != ” is used to check inequality of the strings.

There probably is no universally correct way to achieve this. If you are trying to compare versions in the Debian package system try dpkg --compare-versions <first> <relation> <second>.

compare two strings in dot separated version format in Bash · GitHub, https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-​separated-version-format-in-bash. vercomp() {. [[ $1 == $2 ]] && return 0. Version numbers aren't floating point values; they are .-delimited sequences of integers.42.27 is newer than 42.3, and 42.2.9 could be a valid version number.. Split the version number into its integer components, and compare them "lexiconumerically":

GNU sort has an option for it:

printf '2.4.5\n2.8\n2.4.5.1\n' | sort -V

gives:

2.4.5
2.4.5.1
2.8

compare-two-strings-version-bash • Ye Xing, pure Bash version can compare two strings in dot separated version "Pass: '$1 $op $2'" fi } # Run tests # argument table format: # testarg1  Even assuming your test with > worked, your test is checking whether the version is exactly 5.2.13 (because you're attempting to look for greater than 5.2.13 or less than 5.2.13).

Well if you know the number of fields you can use -k n,n and get a super-simple solution

echo '2.4.5
2.8
2.4.5.1
2.10.2' | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -g

2.4.5
2.4.5.1
2.8
2.10.2

How to compare version values in shell script?, Hi, I need to compare two versions values and report only true or false depending need to break the version string into numeric components and compare each <versionnr1> <versionnr2> # with format for versions xxx.xxx.xxx # returns: 0 if  Here is a simple Bash function that uses no external commands. It works for version strings that have up to three numeric parts in them - less than 3 is fine as well. It can easily be extended for more. It implements =, <, <=, >, >=, and != conditions.

Compare two Version numbers, In this problem we are given two version numbers, we need to compare them and compare them directly because of dot but the versions can compared numeric part Traverse through strings and separate numeric part and compare them,  Compare two string and get "exact" difference only Hi all; Pretty green to perl programming; been searching high and low for a perl (preferably) or unix script that will compare 2 CSV strings in the same file that are separated buy the "|" character (so basically they're side by side) and give the results of ONLY the exact change; note that 19

command line, Instead of comparing version numbers, you could test directly for the -compare-​two-strings-in-dot-separated-version-format-in-bash # Map  To compare two dates in bash, you can use the unix timestamp value of both date using the date command line or the printf method. You can also do a lexicographical comparison of the two dates string by using the double square bracket conditional construct [[..]] .

Text Processing Commands, Topological sort, reading in pairs of whitespace-separated strings and sorting according to input patterns. The original This is a more efficient version of the "​wf2.sh" script. # Check for -tu4 option selects unsigned decimal format for output. bash$ grep -n Linux osinfo.txt 2:This is a file containing information about Linux.

Comments
  • No, don't do it with bc. It's text not numbers. 2.1 < 2.10 would fail this way.
  • Could you state explicitly license of this code snippet? Code looks perfect but I'm not sure if I can use it in AGPLv3 licensed project.
  • @KamilDziedzic: The license terms are stated at the bottom of this page (and most others).
  • gnu.org/licenses/license-list.html#ccbysa Please don't use it for software or documentation, since it is incompatible with the GNU GPL :/ but +1 for great code
  • this fails '1.4rc2 > 1.3.3'. notice the alphanumeric version
  • @SalimaneAdjaoMoustapha: It's not designed to handle that type of version string. I don't see any other answers here that can handle that comparison.
  • Nice solution. For Mac OSX users, you can use GNU Coreutils gsort. That's available through homebrew: brew install coreutils. Then the above should just be modified to use gsort.
  • I got it working in a script in Ubuntu precise by removing -e from echo.
  • Doesn't work with e.g. Busybox on an embedded Linux system, because Busybox sort doesn't have -V option.
  • It's better to use printf instead of echo -e.