3

All,

I have 2 version strings, e.g. "2.0.13" and "2.0.2". I need to compare the 2 versions and determine which is the higher version.

How does one do that using bash?

3 Answers 3

5

You can use sort -V (version sort):

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

results in:

2.0.2
2.0.13
Sign up to request clarification or add additional context in comments.

1 Comment

+1: I was going to write an inline Perl script but that is really neat! I didn't know the -V option.
1

From bmk

You can use sort -V (version sort)

That's the best answer if it works, but unfortunately not all sort commands have the -V option.

If yours doesn't, you'll have to switch to Perl. Newer versions of Perl allow for certain variables to be declared as versions by prefixing them with a lowercase "v". You can then compare them with the gt operator.

See Perldoc perldata for more detail.

Comments

-1

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

  • -t . to make sort split fields on full stops
  • -n to make sort sort numerically
  • -k to define the fields to sort on

But i can't make it work!

1 Comment

Yes, I also tried to get it to work this way. But without success :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.