Work

Create An Awesome Command Line Calculator

When you spend as much time at the terminal as I do, having a calculator at your fingertips can be a really useful feature, and the Command-Line Fu website tells us how to create one easily.

To create the calculator, you can just enter this function on the command line to test out how it works, or add it into our ~/.bashrc file to make sure that it’s always available after restarting the terminal.

calc(){ awk “BEGIN{ print $* }” ;}

To use it, you can simply type calc followed by the calculation you want to solve, making sure to use quotes around the calculation if there are spaces or special characters the shell can’t handle. For instance, if you entered calc 3*99/7+18 at the prompt, you’d get 60.4286 as the answer. For more complex calculations, you can use parenthesis, but make sure to put quotes around it, like this:

calc “((3+(2^3)) * 34^2 / 9)-75.89″

It’s an extremely useful tip for those of us that live at the terminal. It should work on any OS running the bash shell, including Linux, Mac OS X or even Windows with Cygwin installed.

command-line calculator [via TinyHacker]

Comments (AU Comments | US Comments)

  • Wobble

    Can anyone recommend and good Windows Calculators?

    • VinnieD

      Try set /a from DOS prompt. Set /? will give you the operators

      e.g.
      set /a 5*5
      25

  • chakkerz

    echo “((3+(2^3)) * 34^2 / 9)-75.89″ | bc ?

    • Jormundgard

      My thoughts exactly, chakkerz

  • passivekid

    Moffsoft Calculator (http://www.moffsoft.com/) is pretty good Windows alternative calculator. I am an accountant and use it daily. :)

Post Your Comments

Got something to say? There are two ways to comment:

1. Guests

Click here to comment instantly.

2. Facebook Users

Click below to comment using your Facebook account.

We're looking for comments that are interesting, substantial or highly amusing. If your comments are excessively self-promotional, obnoxious, or even worse, boring, you will be banned from commenting. All comments are moderated.