Inside of [[ ]]
, the parentheses don't trigger an arithmetic context, they're interpreted purely for grouping/precedence1. All of these are equivalent:
$ [[ 0 < 1 ]] && echo yes
yes
$ [[ (0 < 1) ]] && echo yes
yes
$ [[ ((0 < 1)) ]] && echo yes
yes
$ [[ (((0 < 1))) ]] && echo yes
yes
$ [[ ((((0 < 1)))) ]] && echo yes
yes
If we have unbalanced parentheses, Bash complains about that:
$ [[ ((((0 < 1))) ]] && echo yes
bash: unexpected token `]]', expected `)'
bash: syntax error near `]]'
This all being said, <
and >
within [[ ]]
are for lexicographical string comparison, so the statement above just checks if 0
is lexicographically sorted before 1
(it is).
Observe:
$ [[ 11 < 2 ]] && echo yes
yes
To compare numbers, you have to use -gt
, -lt
, -ge
, -le
, eq
, ne
instead of >
, <
, >=
, <=
, =
, !=
:
$ [[ 11 -lt 2 ]] && echo yes || echo no
no
Since you're already using (( ))
, you could use just that for comparing numbers:
$ (( 11 < 2 )) && echo yes || echo no
no
This is the simplest and clearest method in my opinion, if you know you have Bash at your disposal.
1 See the manual about [[ ]]
:
Expressions may be combined using the following operators, listed in
decreasing order of precedence:
( expression )
Returns the value of expression
. This may be used to override the normal precedence of operators.
Hat tip to glenn jackman for pointing to the manual in his comment to another answer.