Reputation: 11
echo Enter 2 values:
read val1 val2
c = `expr $val1 + $val2`
echo $c
While executing the shell script, I am getting the following problem:
addition.sh: 3: addition.sh: c: not found
Please help!!
Upvotes: 1
Views: 108
Reputation: 290235
This is because you put whitespace between the variable 'c' and the '='. Hence, the shell assumes c
is a command and =
, and expr $val1 + $val2
are parameters given:
So instead of
c = `expr $val1 + $val2`
^ ^
write
c=$(expr $val1 + $val2)
All together:
echo Enter 2 values:
read val1 val2
c=$(expr $val1 + $val2)
echo $c
Note that you can also get the result of the sum with:
echo $(( val1 + val2 ))
As a general rule, use var=$(command)
to save a command output in a variable.
Upvotes: 4