Inside [[...]]
<
is for string comparison.
So [[ 3.56 < 2.90 ]]
or [[ (3.56 < 2.90) ]]
or [[ ((3.56 < 2.90)) ]]
or [[ (((3.56 < 2.90))) ]]
... is just comparing the 3.56
string with the 2.90
string lexically (and lexically, 3
is greater than 10
for instance).
For integer comparison, it's [[ 3 -lt 2 ]]
or (( 3 < 2 ))
. If you want floating point comparison, you need ksh93
, zsh
or yash
or an external utility like awk
or perl
; bash
can't do it.
You could for instance define a function like:
compare() (IFS=" "
exec awk "BEGIN{if (!($*)) exit(1)}"
)
Which you could use for instance like:
if compare '1.5*10 < 1e3'; then
echo less
fi
Or even for that matters:
if compare '"bar" < "foo"'...
to do string comparisons.
Do not pass uncontrolled externally provided data to that compare
function as it would constitute a command injection vulnerability (the data is interpreted as awk
code, awk
can run commands with its system()
for instance).