How to use bc, the calculator tool

You can do calculation stuffs easily with bash on the command line, however it isnt very intuitive. Bash still works well with some simple calculations but in case you want to perform more complicated calculations, this may become a real hassle. And that is why bc is a better tool for you to perform calculating on the command line. The tool bc can do much more than bash for you and its syntax is ways easier.

As far as I know, bc is a pre-included tool in all Linux distributions so you can use it whenever you want without installing anything.

There are three ways to use bc. The first one is to run it as a shell prompt where you can insert your operations to get the result. Just open the terminal and type "bc" and then you can do what you need, and type "quit" to quit bc.

The second way to use bc is by using STDIN. For example, here is a simple command:
 echo '1+2*3/4' | bc  

After you hit enter, you will get the result in the terminal.

The third way to use bc is to use files. Just write down your bc script into a file then call it:
 bc /path/to/file  

Basic math operations to do with bc

You can run all the basic math operations with bc. With the basic operations like addition, subtraction,  multiplication, division ... you can just open bc as a shell prompt and do everything as with a simple calculator.

However, one thing to notice is that by default, the number of digits which follow the decimal point in the result is 0. Which means if you want to get the square root of 10 with this command:
 echo 'sqrt(10)' | bc  

The result you get will be "3". To get the more exact result, you will have to change the scale variable first. For example, to get the result of square root of 10 with 10 digits following the decimal point, the command you can use is:
 echo 'scale=10;sqrt(10)' | bc  

With this command, you will have the result as "3.1622776601"

If you use bc as a shell prompt, you will need to run the scale command first before doing the calculation.

ibase & obase

ibase and obase are special variables which define output and input base. They are usually used to convert numbers. For example, to convert decimal digits to binary, in the shell prompt of bc, you can type:
 ibase = 10  
 obase = 2  
 23  

And you will get the result "10011"

To convert decimal number to hexadecimal using the STDIN method, you can use the command:
 echo 'ibase=10;obase=16;42' | bc  

And you will get the result "2A"

The pre-defined math functions

bc also has several basic math functions like sine - s(x), cosine - c(x), a(x) for the arctangent, l(x) for the natural logarithm, e(x) for raising the Euler constant to the power of x and j(n, x) for the Bessel function. However, you will have to invoke the library option ( -l) first to use these functions. Or else you will get an error. To use these math functions, just run "bc -l" instead of "bc" when doing the maths. These math functions will calculate their results to the scale set at the time, 20 as default, of their call.

For example, to find sine of 3 ( in radians ), the command you can use is:
 echo 's(3)' | bc -l  

Define your own functions

Just like any normal programming language, bc let you define your own functions. For example, in the shell prompt of bc, you can define a function, say mul3(x), to multiple a variable by 3 like this:
 define mul3(x) { return x*3 }  

Then you can just call the function to multiple 10 by 3:
 mul3(10)  

To get "30" as the result
linux tips and tricks