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

    Can anyone recommend and good Windows Calculators?

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

      e.g.
      set /a 5*5
      25

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

      My thoughts exactly, chakkerz

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

    I loved that calculator and RPN it's what I still use. My wife and I (who both sueidtd physics in the 80 s) have three: our two from college, and one we picked up from a friend several years back in case one of ours should break.Seriously, this was the best calculator ever, and I still get a little confused on non-RPN calculators at first when I have to use one.

Join the discussion!

Trending Stories Right Now