I try to assign two numbers (actually these are the outputs of some remote executed command) to 2 different variables, let say A and B.
When I echo A and B, they show the values:
echo $A
809189640755
echo $B
1662145726
sum=`expr $A + expr $B`
expr: non-integer argument
I also tried with typeset -i but didn't work. As much as I see, bash doesn't take my variables as integer. What is the easiest way to convert my variable into integer so I can add, subtract, multiply etc. them?
Thanks.
First, you should not use expr twice. So
sum=`expr $A + $B`
should work. Another possibility is using pipeline
sum=`echo "$A + $B" | bc -l`
which should work fine even for multiplications. I am not sure how would it behave if you have too large numbers, but worked for me using your values.
You should be able to do
expr $A + $B
or
$(( $A + $B ))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With