I was writing a bash script and suddenly this behaviour started:
[[ 1 < 2 ]]; echo $? # outputs 0
[[ 2 < 13 ]]; echo $? # outputs 1
but -lt
works soundly:
[[ 1 -lt 2 ]]; echo $? # outputs 0
[[ 2 -lt 13 ]]; echo $? # outputs 0
did I accidentally overwrite <
somehow?
here is a script I wrote to test this behaviour:
#!/bin/bash
for a in {1..5}
do
for b in {1..20}
do
[[ $a < $b ]] && echo $a $b
done
echo
done
here is the output:
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 20
3 4
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9
5 6
5 7
5 8
5 9
changing <
to -lt
in the script gives normal output (5 10
shows up for example).
Rebooting did not change anything.
My bash version is GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu). I am on Ubuntu 15.10. I don't know what other information is relevant here.
<
operation, you are making a string comparison, whereas-lt
operator is numeric comparison, If you look at the results you have listed, you will realize it. Numerically 2 is less than 10, alphabetically, the other way around.