More conveniently
This can be done more conveniently using Bash's numeric context:
if (( $(echo "$num1 > $num2" |bc -l) )); then
…
fi
Explanation
Piping through the basic calculator command bc
returns either 1 or 0.
The option -l
is equivalent to --mathlib
; it loads the standard math library.
Enclosing the whole expression between double parenthesis (( ))
will translate these values to respectively true or false.
Please, ensure that the bc
basic calculator package is installed.
Caveat: Exponential notation should be written as *10^
; not E
, nor e
.
For example:
$ echo '1*10^3==1000" |bc
1
Whereas
$ echo '1E3==1000" |bc
0
Strategies to overcome this bc
limitation are discussed here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…