bc utility is surely one of the underdogs when it comes to calculations on command line. It’s a feature rich utility that has its own fan base but needs time to get comfortable with. Here, in this article, structured in form of Q&A, let’s get comfortable with bc utility by discussing both basic and advanced features of this command line calculator.
bc – command line calculator
Q1. I am new to bc, how do I use it for basic calculations?
Ans. It’s very easy. Look at the example shown below :
$ bc <<< 5*4 20 $ bc <<< 5+4 9 $ bc <<< 5-4 1
So you can see that it’s so very easy to do basic calculations with bc.
There is another way of doing the same by using echo command.
Here are some examples:
$ echo "5*4" | bc 20 $ echo "5+4" | bc 9 $ echo "5-4" | bc 1
So you can see that, for simple and quick calculations, just use echo and pipe it to bc command.
NOTE – If you have some expressions in a file that are ready to be calculated then you can also use bc to calculate those expressions. Here is an example :
$ cat calcFile 5+5 6+7 $ bc < calcFile 10 13
So we see that bc is flexible enough to accept expressions from files.
Q2. Can I use the result of last calculation within the same expression that is passed to bc?
Ans. Yes, you can do this. Here is an example of the same :
$ echo "5-4;last+6" | bc 1 7
So you can see that the command line calculator bc uses the result of the first calculation ’5-4′ through the variable name ‘last’ in the second calculation ‘last+6′. Note that it displays the results separately for both the calculations.
NOTE – You can also use a dot (.) instead of the variable name ‘last’. Here is an example :
$ echo "5-4;.+6" | bc 1 7
So you see that dot (.) also holds the value of last calculation.
Q3. How do I store the result of complete operation in variable?
Ans. This is not specifically related to bc, but still, here is how you do it :
$ x=`echo "5-4" | bc` $ echo $x 1
So we see that by using back ticks you can store the result in a variable.
Q4. The approach shown in Q2 can get a bit messy if large number of calculations are there. Is there a cleaner approach?
Ans. Yes. The concept of Linux HERE documents can be used in this case. Here is an example :
$ bc << HERE > 1+4 > 4-1 > 2*4 > HERE 5 3 8
So this way multiple calculations can be done without creating mess in a single line.
RECOMMENDED BY READERS
Q5. When I try to get square root of 10 using bc, I get ’3′ as output. This is wrong, isn’t it?
Ans. Well, its due to the fact that bc will ignore the digits after decimal and then produce the output. You can use the ‘scale’ variable to get the desired result.
Here is an example :
$ echo "sqrt(10)" | bc 3 $ echo "scale=1;sqrt(10)" | bc 3.1 $ echo "scale=10;sqrt(10)" | bc 3.1622776601
So we see that the value assigned to variable ‘scale’ decides the number of post decimal digits. If ‘scale’ is not specified then number of post decimal digits are taken as 0.
Q6. I work with network packets where most of the information is in hexadecimal. Can I use bc to convert values from hexadecimal to decimal and vice versa?
Ans. Ofcourse. You can use bc to convert from hex to decimal, decimal to hex, binary to decimal, decimal to binary, binary to hex and hex to binary. To do this, you have to use the variables ‘ibase’ and ‘obase’.
ibase represents the input base while obase represents the output base. In layman’s term, ibase is used only in case if input number is non decimal. It is 2 for binary inputs and 16 for hexadecimal inputs. For decimal, though the value for ibase is A but it is rarely used as bc treats input values as decimal by default.
Similarly, through obase, bc comes to know in which format the output should be produced. The values for obase are same ie A for decimal, 2 for binary and 16 for Hexadecimal.
Here are some examples :
-> Convert decimal to hexadecimal
$ echo 'obase=16;128' | bc 80
-> Convert hexadecimal to decimal
$ echo 'ibase=16;obase=A;80' | bc 128
-> Convert decimal to binary
$ echo 'obase=2;128' | bc 10000000
-> Convert binary to decimal
$ echo 'ibase=2;obase=A;10000000' | bc 128