Mensaje de error durante la ejecución del comando expr: expr: argumento no entero

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?

Gracias.

preguntado el 27 de noviembre de 13 a las 01:11

My issue resolved. So the thing was, remote command I executed also added a return carriage at the end of the number. So I needed to remove it with printf. After that expr worked. -

4 Respuestas

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.

respondido 27 nov., 13:04

Deberías poder hacer

expr $A + $B

or

$(( $A + $B ))

respondido 27 nov., 13:01

It didn't work. This time a different error message though. sum=$(( $A + $B )) ")809189640755: invalid arithmetic operator (error token is " - JavaRojo

Try in linux bash:

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

respondido 27 nov., 13:05

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

Respondido 28 ago 19, 13:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.