LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2008, 12:56 PM   #1
krap
LQ Newbie
 
Registered: May 2008
Posts: 25

Rep: Reputation: 15
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
 
Old 08-09-2008, 01:04 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
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.

Last edited by weibullguy; 08-09-2008 at 01:16 PM.
 
Old 08-09-2008, 01:06 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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))
 
Old 08-09-2008, 02:05 PM   #4
krap
LQ Newbie
 
Registered: May 2008
Posts: 25

Original Poster
Rep: Reputation: 15
thanx a lot......it starts working
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript adding two numbers. antis Programming 2 07-24-2008 06:53 AM
Adding non-integer numbers stored in variables as strings jeriryan Linux - Newbie 4 07-01-2008 06:16 AM
adding binary numbers in java please help!! trscookie Programming 9 02-28-2006 04:16 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 06:25 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 10:18 AM


All times are GMT -5. The time now is 01:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration