I understand that it is a string comparison, however i dont understand how "abc" & "123" are compared against each other. Thanks in advance!
Asked
Active
Viewed 438 times
2
-
1see http://stackoverflow.com/questions/11989373/bash-testing-if-a-string-is-greater-than-another-how-does-it-work-internal – zeppelin Feb 11 '17 at 21:55
-
The strings are compared against each other lexicographically — look up what that means. – G-Man Says 'Reinstate Monica' Feb 11 '17 at 22:27
1 Answers
7
That's because, inside [[…]], the operators < and > do string comparison.
In doing an string comparison the order is given (basically) by the alphabet, where a is before (smaller) than b and smaller than c: a < b < c.
Numbers are usually before (smaller) than letters: 1 < 2 < a
So, as an a sorts after a 1 abc is greater than 123:
$ [[ abc > 123 ]] && echo yes
yes
Or, as you present it:
$ x=abc y=123
$ [[ $x > $y ]] && echo yes
yes
Jeff Schaller
- 66,199
- 35
- 114
- 250