Monday, October 26, 2009

Better Console Calculator Using bc

If we use expr for mathematical calculations on console (terminal), it is frustrating, and hard to remember syntax and escape sequences used in expr. :( This small tip will help us.

Just add following line in your .bashrc file (~/.bashrc).

function calc
{
echo "${1}" | bc -l;
}

Then update the shell environment by:

$ source ~/.bashrc


"calc" from the shell will work as follows:

$ calc 2+3
5
$ calc 1+2*3
7
$ calc 1.1*2
2.2
$ calc "(1+2)*3"
9
$ calc "(1+2)^3"
27
$ calc "s(.5)"
.47942553860420300027

No comments: