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.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState