Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm learning Shell scripting for a diploma in IT I'm currently doing. I'm trying to write a small script that adds two numbers as shown as in one of the tutorials we were given.

echo "Enter two numbers"
read num1 num2
sum = 'expr $num1 + $num2'
echo "The sum is = $sum"

However when I give it the execution permission and run the script, it gives me this error.

sum: =. No such file or directory.
sum: expr $num1 + $num2: No such file or directory

enter image description here

I tried running this on both Ubuntu and Fedora but same error occurs. Can anyone please tell me what I'm missing here?

share|improve this question
    
Related: unix.stackexchange.com/questions/40786/… –  Bernhard Aug 8 '12 at 5:58

5 Answers 5

up vote 8 down vote accepted

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))
share|improve this answer
    
I got it working. Thank you :) –  Isuru Aug 8 '12 at 4:46
3  
If the motivation for using expr instead of $((...)) is a desire to be portable to classic Bourne shell, then it's a good idea to avoid $(...) as well. –  Alan Curry Aug 8 '12 at 4:55
2  
@AlanCurry are there any shells not supporting $()? As far as i know it is required by posix, e.g. all posix compatible shells should support $() –  Ulrich Dangel Aug 8 '12 at 5:27
    
@UlrichDangel The original Bourne shell didn't have $(…), but it has about died out (maybe someone somewhere is still running /bin/sh on Solaris). –  Gilles Aug 8 '12 at 22:33
    
The C Shell doesn’t support $(…) –– or at least, not all versions. –  Scott Jan 11 '13 at 22:32

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).

share|improve this answer
#!/bin/bash
function add()
{
sum=`expr $a + $b`
echo "Sum is :$sum";
}

echo "Enter the value of a";
read a
echo "Enter the valure of b";
read b
add
share|improve this answer
    
also add some description about code and how will it help to solve the question? –  Tejas 23 hours ago

You can use a single backtick (the ` key)

share|improve this answer
1  
It is a backtick, and it is recommended to use $( ... ) instead in the shells. Besides, the problem is that OP uses spaces where they shouldn't. –  vonbrand Mar 9 '13 at 13:00
echo "addition of two number"

echo "enter a number"
read a
echo "enter a number"
read b

c=`expr $a + $b`

echo $c
share|improve this answer
3  
This is not answering the question. Some explanation would help. –  Bernhard Aug 27 '12 at 5:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.