0

I have written shell script to substract two float values For eg. below are the two values :

debit_amount=7.853117806000353E7
credit_amount=3223649.619999993

val1=$(printf "%f", "$debit_amount")
val2=$(printf "%f", "$credit_amount")

echo " val1 = " $val1
echo " val2 = " $val2

final=`echo "$val1-$val2" |bc`

Output :

 val1 =  78531178.060004,
 val2 =  3223649.620000,
(standard_in) 1: syntax error

I got the above error. I tried below commands as well

final=$(echo "$val1 - $val2"|bc)
echo "$(($val1-$val2))"
echo `expr $val1 - $val2`

However I am still getting syntax error. Am I missing anything?

1

Remove the commas in the printf calls:

val1=$(printf "%f" "$debit_amount")
val2=$(printf "%f" "$credit_amount")

It's not necessary to delimit the arguments by comma. That's why the input for bc looked like this:

78531178.060004,-3223649.620000,

which bc cannot interpret.


BTW: You should not use the backticks (`) anymore:

final="$(echo "$val1 - $val2" | bc)"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.