Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message during the expr command execution: expr: non-integer argument

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.

like image 208
JavaRed Avatar asked Nov 27 '13 01:11

JavaRed


2 Answers

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.

like image 83
Meligordman Avatar answered Sep 23 '22 15:09

Meligordman


You should be able to do

expr $A + $B

or

$(( $A + $B ))
like image 39
startswithaj Avatar answered Sep 25 '22 15:09

startswithaj