LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-23-2012, 06:15 AM   #1
venkatesh51
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Rep: Reputation: Disabled
Not working echo 'expr $a + $b' in linux 5.0.


Hi all,

Please help me in this small issue, i tried 2-3hrs to find the solution but not abled filnally.
Q: Not working a=20 b=10 echo 'expr $a + $b' in linux 5.0 but working without echo?
 
Old 10-23-2012, 07:06 AM   #2
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 around the expr command:
Code:
echo `expr $a + $b`
If using bash you can use the "new" syntax for command substitution, that is $(command) in place of the backticks. Moreover, you may consider the arithmetic operator $((expression)), e.g.
Code:
echo $((a + b))
If compatibility with the older Bourne shell /bin/sh is requested, then stick with backticks and expr.
 
1 members found this post helpful.
Old 10-23-2012, 07:36 AM   #3
venkatesh51
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
thanks it is working

Quote:
Originally Posted by colucix View Post
You have to use backticks not single quotes around the expr command:
Code:
echo `expr $a + $b`
If using bash you can use the "new" syntax for command substitution, that is $(command) in place of the backticks. Moreover, you may consider the arithmetic operator $((expression)), e.g.
Code:
echo $((a + b))
If compatibility with the older Bourne shell /bin/sh is requested, then stick with backticks and expr.
Thanks it is working....
 
Old 10-23-2012, 08:01 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The best thing to do is not use back ticks, because they are deprecated for this very reason.

Code:
echo $(expr $a + $b)
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] test expr VS [ expr ] ashok.g Programming 3 12-03-2009 11:15 PM
echo to ttyS0 not working dan12345 Linux - Newbie 6 03-03-2009 10:27 PM
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) lumix Linux - General 5 02-25-2009 05:31 PM
echo \n not working. suneel Linux - Newbie 2 02-03-2006 07:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:09 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
Open Source Consulting | Domain Registration