Work the Shell - Solve: a Command-Line Calculator

 in
Use bc as a quick-and-dirty command-line utility.
______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

dc Works!

Anonymous's picture

So, your gripe with dc is that you can't do what, exactly? Expect it to evaluate expressions in your format when it clearly is expecting RPN? That's basically equivalent to someone sending you an exe file- "everyone uses it, you should be able to as well".
As far as not evaluating the expression on the command line, you can alias 'dc -e' to 'solve' or whatever. The only flaw with this method is that you must enclose your expression in quotes if it has spaces, but a Linux user should really be able to figure this out on his own. If you want to see 10 decimals of accuracy, make your alias 'dc -e 10k -e'. Read the manpage; you should see how to add constants such as pi and e in as register values.

Better way: CLAC

Mark Borgerding's picture

Since the time of this article, a better alternative to bc/dc has emerged.

The clac (Command Line Advanced Calculator) utility evaluates mathematical expressions from the argument list or from stdin and writes the answer(s) to stdout.
Unlike other command line calculators, clac has infix (natural order) expression syntax, is quite comfortable with complex numbers, defines a great many functions and constants by default, and allows easy definition of new user functions and constants using Python.

If it is not already in your favorite distro's repository, you can download and run it easily. All it needs is python.

thanks but ...

Anonymous's picture

if u need decimal point numbers how about doing
bc -l
??

"With this kind of burnout

Anonymous's picture

"With this kind of burnout on a rudimentary math task..."

Wow. Maybe the burnout is the user, not the tool.

Once there are more than two operations and 3 number, I find the RPN is much easier to use and debug.

And no wrapper is needed. :)

RPN

irlandes's picture

Actually, I like dc. I use it for all sorts of things. It is nice for my checkbook and expense journal because all numbers are left on the screen for visual double checking. I also like the hundreds of digits capacity, for the fun of it. I suppose part of the difference is I understand RPN, heh, heh.

Also, my Linux machine has dosemu, which means I can run Qbasic if I need more complex calculations. I used an early HP-25 with RPN in the 70's. My HP-20S, which does not use RPN, worked great for impedance calculations on black boxes for the B-2 before I retired in 1997. It needs new batteries.

Friends in Mexico had no idea what I meant by a slide rule, and I bumped into my old one in my filing cabinet, I will be taking it with me Friday when I go there, to show them what one is. A real relic, yes?

cool thanks!

Anonymous's picture

cool thanks!

I always use this great

Mike 2008's picture

I always use this great Command Line Calculator

http://www.fnoware.st/?CLC-linux

a windows version does also exists

excellent post, based on it

Anonymous's picture

excellent post, based on it I was able to add this to a function (or alias) too

#
# foo(){echo "scale=4;$@" | bc -l; }
#
# alias bar='bash -c '\''echo "scale=4;$0 $@" | bc -l '\'''
#

thank you.

Cool tip

Dirk Gently's picture

thanks for the tip. I have spent a good amount of time before reading the bc man and looking into other ways to do this. Thanks alot.

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions