LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 11:56 AM   #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, 12:04 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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 12:16 PM.
 
Old 08-09-2008, 12:06 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 01:05 PM   #4
krap
LQ Newbie
 
Registered: May 2008
Posts: 25

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


Reply



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 05:53 AM
Adding non-integer numbers stored in variables as strings jeriryan Linux - Newbie 4 07-01-2008 05:16 AM
adding binary numbers in java please help!! trscookie Programming 9 02-28-2006 03:16 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:49 AM.

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
Open Source Consulting | Domain Registration