mrDoc's blog

When You Need a Calculator


March 4, 2006 - 3:14pm

The neat calendar CLI tip recently posted reminded me of a handy CLI calculator I use frequently. Awk can function as a calculator when fed a simple algebraic expression such as 1+2, 1/3 or 2^30 (if you can’t remember how many bytes are in 1 GB!).

Adding a function to the .bashrc file such as

calc() { 
    awk "BEGIN {print $* ; }"
}

Gives a simple command line calculator that can handle floats without the limitations of bc or dc. The format can be cleaned up by adding a format statement to the output:

calc2() {
	awk "BEGIN { CONVFMT = \"%.10g\" ; OFMT =\"%.10g\" ;  print $* ; }"
}

Handy CLI tools can be easily created using the standard programs on the systemThere's more »

Syndicate content