Bash string comparison -
i trying compare 2 strings in bash script , getting strange results.
if [[ "010" < "01." ]]; echo "wrong"; else echo "ok"; fi if [[ "010" < "01.0" ]]; echo "wrong"; else echo "ok"; fi if [ "010" \< "01." ]; echo "wrong"; else echo "ok"; fi if [ "010" \< "01.0" ]; echo "wrong"; else echo "ok"; fi reading documentation seemed [[ < ]] , [ \< ] should work same, don't. it seems [[ < ]] works wrong when strings don't have same length. missing something?
edit: expected result 4 x ok. tested on:
- centos release 6.4 (final) - gnu bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) (
ok wrong ok ok) - ubuntu 14.04.2 lts - gnu bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu) (
ok wrong ok ok) - opensuse 13.1 (bottle) (x86_64) - gnu bash, version 4.2.53(1)-release (x86_64-suse-linux-gnu) (
ok ok ok ok)
here documentation help test:
string1 > string2
true if string1 sorts after string2 lexicographically.
taking first if statement example:
if [[ "010" < "01." ]]; echo "wrong"; else echo "ok"; fi in bash string "01." sorts lexicographically before string "010" (you can test in other tools microsoft excel), comparison returns false. case 4 of comparisons.
note adding additional 0 end of "01." doesn't change ordering versus "010", still same result.
Comments
Post a Comment