How to compare two floating point numbers in Bash?

前端 未结 17 2141
無奈伤痛
無奈伤痛 2020-11-22 02:17

I am trying hard to compare two floating point numbers within a bash script. I have to variables, e.g.

let num1=3.17648e-22
let num2=1.5

No

17条回答
  •  长情又很酷
    2020-11-22 02:40

    bash handles only integer maths but you can use bc command as follows:

    $ num1=3.17648E-22
    $ num2=1.5
    $ echo $num1'>'$num2 | bc -l
    0
    $ echo $num2'>'$num1 | bc -l
    1
    

    Note that exponent sign must be uppercase

提交回复
热议问题