Wednesday, May 27, 2015


Command Line Calculator bc – 11 Extremely Useful Examples.

 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 

  •  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 :

 

No comments:

Post a Comment