LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   error in script for adding two numbers (https://www.linuxquestions.org/questions/linux-newbie-8/error-in-script-for-adding-two-numbers-661621/)

krap 08-09-2008 11:56 AM

error in script for adding two numbers
 
hey ,

i have wrote a script to add two numbers which looks something like this

x=10
y=10
z='expr $x + $y'
echo $z

according to me output should be 20 but output is coming expr $x + $y

can someone help me in figuring out the problem

thanks in advance

weibullguy 08-09-2008 12:04 PM

You use the back tick, not the single quote. The back tick is on the key next to the 1 key.
Code:

x=10
y=10
z=`$x+$y`
echo $z

You can also use parentheses
Code:

x=10
y=10
z=$(expr $x+$y)
echo $z

Bookmark this site if you plan to write Bash scripts.

colucix 08-09-2008 12:06 PM

You have to use backticks, not single quotes. Or the $(command) syntax if using bash. Or the arithmetic operator with double parentheses:
Code:

z=`expr $x + $y`
z=$(expr $x + $y)
z=$((x + y))


krap 08-09-2008 01:05 PM

thanx a lot......it starts working


All times are GMT -5. The time now is 12:03 PM.