Tuesday 3 November 2009

Create an Awesome Command Line Calculator [Terminal Tip]

Create an Awesome Command Line Calculator [Terminal Tip]: "

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 web site 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.






"

No comments:

Sike's shared items