One thing I really like about GNU/Linux operating system, is that it has a lot of utilities which are easily to install, free, and secure, something other operating systems does not have.

One of those utilities is bc a handy calculator, with it you can perform easy calculations, or perform complicated calculus, you can even define your own functions, this utility is really useful.

You may say that it is equivalent to the HP you had when you were at University, well I had a Casio :).

There are two ways to use it, from the command line and using its interactive interface.

**Using bc from the command line**

echo "2+2" | bc

4

By default bc uses no zeros after the decimal point, so division may give a not precise answer, in order to have the right or more precise answer you need to specify scale, which means the number of zeros after the decimal point.

echo "scale=3; 5/3" | bc

1.66

Or you can invoke bc with its arithmetic libraries.

echo "5/3" | bc -l

1.66666666666666666666

**Using bc from its interactive interface**

bc also has a good interactive interface, where you can perform normal operations or even declare your own functions.

bc -l file

This way you are loading the arithmetic library and an optional file, where you can define your own functions, which you can also declare in the interactive shell itself, let’s see some examples.

Let’s declare a function to calculate the area of a circle, the formula for this is:

A=pi*r^2

Where: pi=3.141516… r=radius of the circle.

define carea() { print "Circle radius? "; r = read() pi=4*a(1); a=pi*r^2; return(a) }

Now you can call this function by entering the interactive mode and writing:

carea() Circle radius? 34 3631.68110754980098363664

Do not forget to read the bc man page