LinuxQuestions.org
Help answer threads with 0 replies.
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 12-31-2015, 12:03 AM   #1
arivarasan.e@gmail.com
LQ Newbie
 
Registered: Dec 2015
Posts: 4

Rep: Reputation: Disabled
Problem with expr


Hi All,
I need to subtract two variables (say $var1 and $var2)...when I use

echo "`expr ${var1} - ${var2}`"

It throws error.
expr: syntax error

I'm not sure whether I'm using expr correctly or not. Anyone pls help me fix this.
 
Old 12-31-2015, 05:34 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,046

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
bash math ...

arguably, you are using either echo wrong, or the quoting.

simplify. Try something like
Code:
echo `expr ${var1} - ${var2}`
and see what happens.

By the way, the bash shell can do most integer math without expr. Google for details.
 
Old 12-31-2015, 09:47 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,298

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by arivarasan.e@gmail.com View Post
Hi All,
I need to subtract two variables (say $var1 and $var2)...when I use

echo "`expr ${var1} - ${var2}`"

It throws error.
expr: syntax error
It should work. Perhaps var1 and var2 aren't set the way you think they are. Try running "set -x" first so you can see what is actually being executed.
Code:
$ var1=2
$ var2=3
$ set -x
$ echo "`expr ${var1} - ${var2}`"
++ expr 2 - 3
+ echo -1
-1
$ set +x
 
  


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
[SOLVED] test expr VS [ expr ] ashok.g Programming 3 12-03-2009 11:15 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
problem with expr function jadeddog Programming 10 06-29-2008 08:44 PM
Expr in Shell mystical_sun2000 Linux - Newbie 1 02-08-2006 06:59 AM
About how to use expr naihe2010 Programming 2 10-31-2005 06:28 AM

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

All times are GMT -5. The time now is 04:34 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