1
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!!

1
  • Use c=$(( val1 + val2)) instead of c=$(expr $val1 + $val2; the expr command is (mostly) obsolete in modern shells. Commented Jan 28, 2014 at 17:56

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.