ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.