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]

Discuss

(5 Comments)
  • [–]

    Wobble

    Wednesday, November 4, 2009 at 11:27 AM

    Can anyone recommend and good Windows Calculators?

    • [–]

      VinnieD

      Wednesday, November 4, 2009 at 3:27 PM

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

      e.g.
      set /a 5*5
      25

  • [–]

    chakkerz

    Wednesday, November 4, 2009 at 1:45 PM

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

    • [–]

      Jormundgard

      Thursday, November 5, 2009 at 11:53 PM

      My thoughts exactly, chakkerz

  • [–]

    passivekid

    Wednesday, November 4, 2009 at 3:38 PM

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

Join The Discussion