I suspect, without further context, that what was intended was an evaluation.
((a = 1))
if [ a > 2 ]; then echo "Foo"; else echo "bar"; fi
is a bug, as mentioned. [
is a synonym ("syntactic sugar", as Larry Wall likes to call it) for test
, so what is actually being done here is:
let a=1
if test a 1>2 # evaluate a, redirect stdout (there is none) to file "2"
then echo "Foo"
else echo "bar"
fi
Look over the surrounding logic and see if perhaps it should not have been something like this:
((a = 1))
if (( a > 2 )); then echo "Foo"; else echo "bar"; fi
That would be a proper arithmetic evaluation.
$: (( 1 > 2 )) && echo ok || echo no
no
$: (( 20 > 100 )) && echo ok || echo no
no
$: (( 100 > 20 )) && echo ok || echo no
ok
Beware the more subtle bug of using double-square brackets:
((a = 1))
if [[ a > 2 ]]; then echo "Foo"; else echo "bar"; fi
This will, in this case, generally give the expected result, but it is evaluating in string context, probably according to the LOCALE setting. It will often fail when the number of digits differ, as in [[ 10 > 2 ]]
.
$: [[ 1 > 2 ]] && echo ok || echo no
no
$: [[ 10 > 2 ]] && echo ok || echo no
no
$: [[ 2 > 10 ]] && echo ok || echo no
ok