Example of `bc` usage
Example of bc usage

bc, for basic calculator, is an arbitrary-precision calculator language with syntax similar to the C programming language. In this post, we will explore the features of bc and how to use it.

What is bc?

bc is an arbitrary precision calculator language that supports both integer and floating-point arithmetic. It can handle complex mathematical operations, including trigonometric functions, logarithms, and more. bc is often used in shell scripts for calculations that require high precision.

Installing bc

Most Unix-like systems come with bc pre-installed. You can check if it is available on your system by running:

> bc --version

If it is not installed, you can install it using your package manager. For example on Debian/Ubuntu:

> sudo apt-get install bc

Basic Usage

To start using bc, simply type bc in your terminal:

> bc

You will enter the bc interactive mode, where you can start typing your calculations. For example:

> 5+3

Press Enter, and you will see the result:

8

To exit bc, type quit or press Ctrl + D

You can also “pipe” echo output to bc:

> echo '2*6' | bc
12

Working with Variables

bc is programming language by it self, So you can define variables too. Enter bc interactive mode and define your variables in each line:

> a=4
> b=2
> a*b
8

Functions in bc

You can define your own functions in bc. Here’s an example of how to define and use a function:

> bc
> define double(x) {return x*2}
> double(5)
10

Using bc with Files

You can also use bc to read expressions from a file. For example create a file named calc.txt with the following content:

5+3
10/2
4*7

You can then run bc with this file as input:

> bc calc.txt
8
5
28

More advanced calculations

If basic calculation is not enough for you, a standard math library is also available by the command line option -l or --mathlib. If requested, the math library is defined before processing any files. By typing executing bc -l command, you can enter interactive mode with standard math library defined as well:

> bc -l

Now you can calculate a sine (s), cosine (c), arctangent (a), natural logarithm (l), exponential (e) function as well using mathlib:

> bc -l

> s(90)
.89399666360055789051

> c(90)
-.44807361612917015236

> a(90)
1.55968567289728914662

> l(2.71)
.99694863489160953206

> e(1)
2.71828182845904523536

Setting output precision

You can set the output precision by changing scale variable. When running bc with no command line option, By default it’s set to 0. If running with -l option (define mathlib) it’s set to 20 by deafult. we can change it to what ever we want:

> bc -l

> scale
20

> scale=2

> s(90)
.88

> c(90)
-.44

> a(90)
1.55

> l(2.71)
.99

> e(1)
2.71

Conclusion

The bc command is a versatile tool for performing calculations in Unix. With its support for arbitrary precision, variables, functions, and file input, it can handle a wide range of mathematical tasks. Whether you are a developer, a data analyst, or just someone who needs to perform calculations, bc is a valuable addition to your toolkit.

References

[1] https://en.wikipedia.org/wiki/Bc_(programming_language)

[2] https://www.gnu.org/software/bc/manual/html_mono/bc.html