Work the Shell - Solve: a Command-Line Calculator

December 1st, 2007 by Dave Taylor in

Use bc as a quick-and-dirty command-line utility.
Your rating: None Average: 4 (1 vote)

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.

__________________________


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Comment viewing options

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

Cool tip

On February 7th, 2008 Dirk Gently (not verified) says:

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.

Featured Videos

Email is one of the least private and least secure forms of communication, although few people realize this. MixMaster is one way to allow secure, anonymous communication even over the very public medium of email. This tutorial will get you started with MixMaster quickly and easily.

In case you were wondering about the fun side of Linux World Expo, we thought we'd give you a peek at our shenanigans. We at Linux Journal love what we do so much, that we can't help but have a ball wherever we go.

From the Magazine

September 2008, #173

Feeling a bit like a Thermian? Never give up, never surrender! Someday, you could go from underdog to top dog. Just take a look at a few of the underdogs we highlight in this issue: Mutt, djbdns, Nginix, Gentoo, Xara and the program voted mostly likely to fail just a few years back—Firefox. If Firefox not radical enough for you, check out Chef Marcel's column for some more alternatives. Having trouble mapping your program data to your relational database? If so, Rueven Lerner shows you some tricks in his At The Forge column.

Need to run GUI applications on your server in the next state? In his Paranoid Penguin column, Mick Bauer shows you how to do it securely. Kyle Rankin keeps hacking and slashing and shows you a few split screen secrets you may not be familiar with. Finally, we all know what happens next February, but only Doc knows what happens afterward.

Read this issue