Adding two numbers using expr

First you have to get rid of the spaces for the assignment, e.g

sum='expr $num1 + $num2'

then you have to change ' to a ` or even better to $():

sum=$(expr "$num1" + "$num2")

instead of using expr you can also do the calculation directly in your shell:

sum=$((num1 + num2))

You have probably misread backticks as single quotes in the line:

sum = 'expr $num1 + $num2'

See Greg's Wiki on using $(...) instead.

This works as expected:

sum=$(expr "$num1" + "$num2")

Also note there are no gaps around the equals sign (the variable assignment).

Tags:

Shell Script