In bash manual, Section 6.5 Shell Arithmetic says
<= >= < > comparison == != equality and inequality && logical AND || logical OR
Their meanings seem to imply they are used for conditional expressions. But they appear in the section for arithmetic expressions.
So can they be used in conditional expressions?
In Section 6.4, we already have operators for conditional expressions, which are similar to
<=,>=,<,>,==,!=for arithmetic expressions:
arg1 OP arg2
OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
We also already have operators for conditional expressions, which are similar to
||for arithmetic expressions:
Since we have the operators introduced for conditional expressions, why do we need those similar operators introduced for arithmetic expressions?
Can they be used in test commands?
We know that
ifis followed by a test command
The syntax of the if command is:
if test-commands; then consequent-commands; [elif more-test-commands; then more-consequents;] [else alternate-consequents;] fi
[[...]]both take conditional expressions within and create test commands. Can they take arithmetic expressions?
What makes me even confused is when
[[...]]with arithmetic expressions in them are used as test command,
[...]behaves incorrectly, while
$ if [ 1 > 2 ]; then echo h; else echo b; fi h $ if [ 1 < 2 ]; then echo h; else echo b; fi h $ if [[ 1 < 2 ]]; then echo h; else echo b; fi h $ if [[ 1 > 2 ]]; then echo h; else echo b; fi b