7

When I run

if [[ 10 < 2 ]];
  then echo "yes";
else echo "no"; 
fi

in shell, it returns yes. Why? should it be no? And When I run

if [[ 20 < 2 ]];
  then echo "yes";
else echo "no";
fi

it returns no.

Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
rajen
  • 69
  • 1
  • 5
  • When you were asking the question, the interface should have showed a series of search results for similar questions. Were none of those flagged in the header shown there? – Charles Duffy Mar 23 '17 at 16:33

1 Answers1

10

Because you compare strings according to Lexicographical order and not numbers

You may use [[ 10 -lt 2 ]] and [[ 20 -lt 2 ]]. -lt stands for Less than (<). For Greater than (>) -gt notation can be used instead.

In bash double parenthesis can be used as well for performing numeric comparison:

if ((10 < 2)); then echo "yes"; else echo "no"; fi

The above example will echo no

Denis Itskovich
  • 3,798
  • 3
  • 26
  • 48