JavaRed
JavaRed

Reputation: 708

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.

Upvotes: 7

Views: 49193

Answers (4)

Soft Technoes
Soft Technoes

Reputation: 1115

You have to copy and paste this code and run. I hope it will be helpful for you.

echo "enter first number"
read num1
echo "enter second number"
read num2
echo $((num1 + num2))

Save your file as file_name.sh and run it from your terminal

Upvotes: 0

PasteBT
PasteBT

Reputation: 2198

Try in linux bash:

A=809189640755
B=1662145726
echo $((A + B))

Upvotes: 2

Meligordman
Meligordman

Reputation: 75

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.

Upvotes: 5

startswithaj
startswithaj

Reputation: 354

You should be able to do

expr $A + $B

or

$(( $A + $B ))

Upvotes: 1

Related Questions