How to add an integer number and a float number in a bash shell script

前端 未结 4 1690
故里飘歌
故里飘歌 2021-01-03 23:58

I have two numbers:

value1=686
value2=228.35

I am not able to add an integer and a float. Please help me out to get the result.

I a

4条回答
  •  长发绾君心
    2021-01-04 00:44

    If you have the bc language installed, you can do the following:

    #!bin/bash
    numone=1.234
    numtwo=0.124
    total=`echo $numone + $numtwo | bc`
    echo $total
    

    If you don't have bc, then you can try with awk. Just in one single line:

    echo 1.234 2.345 | awk '{print $1 + $2}'
    

    There are plenty of other options, also. Like python, perl, php....

提交回复
热议问题