bash: I broke [[ < ]]

From the bash man page.

When used with [[, the < and > operators sort lexicographically using the current locale.

From the output, it appears to be working as designed.


How about:

for a in {1..5}; 
do     
  for b in {1..20};     
  do         
    (( $a < $b )) && echo $a $b
  done      
  echo
done

According to http://www.tldp.org/LDP/abs/html/dblparens.html

Similar to the let command, the (( ... )) construct permits arithmetic expansion and evaluation. In its simplest form, a=$(( 5 + 3 )) would set a to 5 + 3, or 8. However, this double-parentheses construct is also a mechanism for allowing C-style manipulation of variables in Bash, for example, (( var++ )).


Firstly, [[ is not POSIX and should be avoided.

Secondly, if you wish to use < as part of an arithmetic test you can do this, but with different syntax:

if [ $((2 < 13)) = 1 ]
then
  echo '2 is less than 13'
else
  echo '2 is greater or equal to 13'
fi

Or:

if expr 2 '<' 13
then
  echo '2 is less than 13'
else
  echo '2 is greater or equal to 13'
fi

Tags:

Bash

Test