Reputation: 708
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
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
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