A More Powerful Calculator

bc is an arithmetic utility not limited to just integers:

$ bc
scale=4
8/3
2.6666
2.5 * 4.1/6.25
1.6400
quit
$

In the previous example, you invoke bc and set the scale to 4, meaning that you want it to calculate any fraction to four decimal places. You ask it to calculate 8/3, which gives 2.6666, and then you do a more complex calculation. Note that spaces are optional. Finally, you enter quit to return to the shell prompt. bc can handle addition (+), subtraction (-), multiplication (*), division (/), remainder or modulus (%), and integer exponentiation (^). bc also can accurately compute numbers of any size:

9238472938742937 * 29384729347298472
271470026887302339647844620892264

In addition, bc can be used within backquotes ...

Get Practical UNIX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.