Work the Shell - Solve: a Command-Line Calculator

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


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. :)


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

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.