The bash
shell does not do floating point, it's strictly concerned with integral values (and strings of course, but that's another matter). From the bash
man page (my italics):
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.
You can do these sorts of comparisons using a tool like bc
, which does understand floating point:
pax$ if (( $(bc <<<'3.14159 > 2.71828') )) ; then
...> echo pi is greater than e
...> else
...> echo pi is not greater than e
...> fi
pi is greater than e
pax$ if (( $(bc <<<'3.14159 < 2.71828') )) ; then
...> echo pi is less than e
...> else
...> echo pi is not less than e
...> fi
pi is not less than e
This works because bc
can take a floating point comparison and gives you 1
if it's true, 0
otherwise.
In case of needing comparison with a variable, make sure you use double quotes so the variable is interpreted:
xx=3.14
pax$ if (( $(bc <<<"$xx < 2.71828") )) ; then
...> echo xx is less than e
...> else
...> echo xx is not less than e
...> fi
xx is not less than e
You can also include arbitrary expression within bc
and have bash
just interpret the comparison result of 0
or 1
:
pax$ xx=4 ; if (( $(bc <<<"$xx < 5 || $xx > 7") )) ; then echo not 4-6 ; fi
not 4-6
pax$ xx=5 ; if (( $(bc <<<"$xx < 5 || $xx > 7") )) ; then echo not 4-6 ; fi
pax$ xx=9 ; if (( $(bc <<<"$xx < 5 || $xx > 7") )) ; then echo not 4-6 ; fi
not 4-6