Work the Shell - Solve: a Command-Line Calculator

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

One thing that's always bothered me about Linux, and UNIX before it, is that there isn't a decent command-line calculator available. You know, something where you can type in solve 5+8 or, better, solve 33/5 and get the solution.

There's expr, but that's barely useful at all, and I've always been baffled that it's constrained to integer math to this day. No one has ever extended its functionality beyond the most rudimentary capabilities for shell script programming.

There's bc, which has the power we seek, but it has to be one of the most bizarre interfaces of any program in the Linux panoply, and there's nothing more frustrating than accidentally falling into bc and being unable to get out!

The third choice is dc, the so-called desktop calculator (really, that's what dc stands for), but that too is fundamentally flawed because it uses RPN (reverse Polish notation—really, it's named after Polish mathematician Jan Lukasiewicz). Not sure what that is? Well, here's a demonstration of how it doesn't work:

$ dc 1+1
Could not open file 1+1: No such file or directory
$ dc -e 1+1
dc: stack empty
$ dc -e 1 + 1
Could not open file +: No such file or directory

With this kind of burnout on a rudimentary math task, do you really care about learning an entirely new notation to figure out that 1+1=3? No, 2?

Of these three choices, none suffice, but bc does show promise because it can handle floating-point numbers and has the ability to specify how much post-decimal-point precision you seek. Learn its obscure notation, and you can calculate 1+1:

$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+1
2
quit
$  	

The challenge with bc is to revamp how you interact with it—to put a wrapper program “in front” of the utility so that you can use it as a quick-and-dirty command-line utility.

There are two problems with using it that way as designed, as you can see here:

$ bc 1+1
File 1+1 is unavailable.

and here:

$ bc -q
3/2
1
quit
$

(The -q option gets rid of the FSF intro header.)

By default, bc offers up integer results only, so although you and I know that 3/2 = 1.5, bc shows it as 1, which makes it pretty darn useless for any precision calculations.

However, unlike the other calculation alternatives, bc does have the ability to be a bit more precise. The key is that you have to specify the scale, the number of digits after the decimal point that you want to see. Add that, and things change:

$ bc -q
11/7
1
scale=8
11/7
1.57142857
quit
$

The challenge for us is to figure out a way to write a shell script wrapper that allows us not only to do simple calculations from the command line, but also have them solved as floating-point calculations. The goal is to be able to type something like solve 11/7, and have it display 1.57142857.

Of Wrappers and Rappers

At this point, given my headline, I have an urge to write in some sort of rhyming slang, but I know my editor won't let me get away with it, so you're safe. Nonetheless, wrappers are an important concept and a big part of why Linux is so darn powerful as an operating system.

In many ways, UNIX and Linux supply all the tools you need, the rudimentary building blocks, and one of the purposes of shell script programming is to add the veneer, the pleasantry of a usable user interface. That's exactly what we're doing with our solve script if you think about it. Actually, doing mathematics in a shell script would be pretty tricky, but we certainly can transform a simple query into the more complicated sequence of commands needed to get bc to output what we desire.

The challenge though is that we're not simply adding a command flag or turning an express around; we need to capture the requested formula and inject it into a sequence of commands that we're feeding the underlying Linux utility via standard input (stdin).

I do this by using what's called a here document, as denoted with the notation << in a script. Recall that a notation like wc < letter.txt invokes the wc command and uses the contents of letter.txt as stdin for the command. The result is the number of characters, words and lines in the file, as if I'd actually typed in the file, letter by letter.

The << notation is a convenient way to have a similar remapping of standard input for the invoked command, but based on the material that's actually present in the command sequence, not a separate file.

As a result, the character sequence immediately following the << symbol is the end marker, not the filename. It works like this:


cat << EndOfInput
This is a sample of the kind of 
trick you can do with a here document. 
Why is this cool?  Because you can also 
expand variables ($PATH) 
and do other spiffo shell script 
hijinks.
EndOfInput

Run this little script snippet (as a script), and you'll see the following:

$ sh samplepscript.sh
This is a sample of the kind of 
trick you can do with a here document. 
Why is this cool?  Because you can also 
expand variables (/bin:/sbin:/usr/bin:/usr/sbin) 
and do other spiffo shell script 
hijinks.

In our case, this also means you can move a command-line argument into the middle of a sequence of commands being sent to a core Linux command like bc. For example:


#!/bin/sh

bc << EOF
scale=4
$@
quit
EOF

Believe it or not, that's the rudimentary solution to our challenge of writing a floating-point-capable command-line calculator. Check it out:

$ sh solve.sh 1+1
2
$ sh solve.sh 11/7
1.5714

Next month, we'll dig into useful refinements and make it a full-blown addition to our Linux toolkit. See you then!

Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com.

______________________

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.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix