LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2011, 07:11 AM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Bash subtract negative number using bc


How can i get bc to subtract negative numbers?
Code:
g  echo $bal $amt
347.87 -742
g  echo "$bal-$amt" | bc
(standard_in) 1: syntax error
g  amt=742
g  echo "$bal-$amt" | bc
-394.13
g
 
Old 05-15-2011, 07:17 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
bc needs a space between the subtraction operator and the unary subtraction operator:
Code:
c@CW8:~$ bal=347.87; amt=-742; echo "$bal-$amt" | bc
(standard_in) 1: syntax error
c@CW8:~$ bal=347.87; amt=-742; echo "$bal-i $amt" | bc
-394.13
EDIT: Oops! vi editing at the command line! Should have been:
Code:
c@CW8:~$ bal=347.87; amt=-742; echo "$bal-$amt" | bc
(standard_in) 1: syntax error
c@CW8:~$ bal=347.87; amt=-742; echo "$bal- $amt" | bc
1089.87

Last edited by catkin; 05-15-2011 at 07:19 AM.
 
Old 05-15-2011, 07:20 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Yopu need spaces around the - in the echo statement:
Code:
$ A="347.87"
$ B="-742"
$ echo "$A-$B" | bc
(standard_in) 1: parse error
$ echo "$A - $B" | bc
1089.87
/Edit: Beaten by catkin
 
Old 05-15-2011, 07:45 AM   #4
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by catkin View Post
bc needs a space between the subtraction operator and the unary subtraction operator:
Code:
c@CW8:~$ bal=347.87; amt=-742; echo "$bal-$amt" | bc
(standard_in) 1: syntax error
c@CW8:~$ bal=347.87; amt=-742; echo "$bal-i $amt" | bc
-394.13
EDIT: Oops! vi editing at the command line! Should have been:
Code:
c@CW8:~$ bal=347.87; amt=-742; echo "$bal-$amt" | bc
(standard_in) 1: syntax error
c@CW8:~$ bal=347.87; amt=-742; echo "$bal- $amt" | bc
1089.87
Thank you. Two consecutive - signs must upset it, as it doesn't require that space separation when the number is unsigned. But its more prudent to use space separation always.
 
Old 05-15-2011, 07:48 AM   #5
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by druuna View Post
Hi,

Yopu need spaces around the - in the echo statement:
Code:
$ A="347.87"
$ B="-742"
$ echo "$A-$B" | bc
(standard_in) 1: parse error
$ echo "$A - $B" | bc
1089.87
/Edit: Beaten by catkin
Thank you for your help.
 
Old 05-15-2011, 07:49 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by porphyry5 View Post
But its more prudent to use space separation always.
That would be a good KISS convention.
 
Old 05-15-2011, 07:56 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by porphyry5 View Post
Thank you for your help.
You're welcome
 
Old 05-15-2011, 08:16 AM   #8
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by catkin View Post
That would be a good KISS convention.
Meaning that space separation is generally true for all linux command-line apps? I find it confusing trying to keep track of situations that require it and those that don't. Learning by example from the net is convenient, but examples that don't use space separation carry the implication that in that particular situation one shouldn't.
 
1 members found this post helpful.
Old 05-15-2011, 09:33 AM   #9
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
In bc it is always safe to enclose quantities in parentheses.

Quote:
foo$ minuend=5
foo$ subtrahend=-5
foo$ echo "$minuend-($subtrahend)"
5-(-5)
foo$ echo "$minuend-($subtrahend)" | bc
10
foo$
 
Old 05-15-2011, 09:52 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by porphyry5 View Post
Meaning that space separation is generally true for all linux command-line apps? I find it confusing trying to keep track of situations that require it and those that don't. Learning by example from the net is convenient, but examples that don't use space separation carry the implication that in that particular situation one shouldn't.
There are few situations where it is wrong to include a space; the only ones that come to mind right now (there are probably others) are bash assignments (foo=bar, not foo = bar), awk function calls (myfunc( ) not myfunc ( ) ) and dd's if=<file name> etc. not if = <file name> (but that's derived from IBM mainframe JCL) and sometimes long options need --<option>=<value>. OTOH there are situations where it is wrong not to have a space such as bash' [[ <test expression> ]] not [[<test expression>]] and bc as you have found out.
 
1 members found this post helpful.
Old 05-17-2011, 11:12 AM   #11
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by catkin View Post
There are few situations where it is wrong to include a space; the only ones that come to mind right now (there are probably others) are bash assignments (foo=bar, not foo = bar), awk function calls (myfunc( ) not myfunc ( ) ) and dd's if=<file name> etc. not if = <file name> (but that's derived from IBM mainframe JCL) and sometimes long options need --<option>=<value>. OTOH there are situations where it is wrong not to have a space such as bash' [[ <test expression> ]] not [[<test expression>]] and bc as you have found out.
Yeah, immediately after I wrote my previous post I tried foo = bar in bash and saw my hopes dashed. But thanks for this list; even if there are other examples this still brings a lot of order to an area that seemed quite chaotic to me.
 
  


Reply

Tags
bc



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
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
[SOLVED] How to subtract two dates in a file to get number of days? btacuso Linux - Newbie 2 07-13-2010 11:33 AM
How can i subtract two non-real (aka decimal'd) numbers via bash onesikgypo Programming 3 04-28-2010 11:55 AM
[SOLVED] How to grep a negative number in a file nasridine Linux - Newbie 3 03-04-2010 08:28 PM
using bc to subtract hexdemical schurt Linux - Software 1 01-07-2005 01:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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