Bash script and strings are compared

All,

I have 2 version lines, for example. "2.0.13" and "2.0.2". I need to compare 2 versions and determine which one is higher.

How to do this using bash?

+3
source share
3 answers

You can use sort -V(version sorting):

echo -e "2.0.13\n2.0.2" | sort -V

leads to:

2.0.2
2.0.13
+5
source

From bmk

You can use sort -V (sort sort)

This is the best answer if it works, but unfortunately not all commands sorthave a parameter -V.

, Perl. Perl , "v". gt.

. Perldoc perldata.

+1

If you haven't -V, I'm sure there should be a way to do this by combining:

  • -t .to make sort separation fields at full stops
  • -nto sort the sorting digitally
  • -kto define fields to sort by

But I can’t make it work!

-1
source

All Articles