Do math in the Linux shell with GNU bc

Do better math in the shell with bc, a mathematical language for advanced calculation.
46 readers like this
46 readers like this

Most POSIX systems come with GNU bc, an arbitrary precision numeric processing language. Its syntax is similar to C, but it also supports interactive execution of statements and processing data from standard in (stdin). For that reason, it's often the answer to the question, "How do I do math in the Linux shell?" This style of response is common online:

$ echo "1+1" | bc

While that's perfectly valid, few users argue it's elegant compared to something more intuitive, such as:

$ 1+1  #this does not work

The interactive mode is a little easier:

$ bc

But interactive modes don't always fit the desired and intuitive workflow of something simple, like just typing in the calculation you want. For this, I suggest Bluebat's calculator in pure Bash.

What bc actually provides is a mathematical language for advanced calculation.

Advanced functions with mathlib

On its own, bc provides basic math functions. You can test them in the interactive mode:

$ bc

Use the --mathlib option to gain advanced functions, including sine, cosine, tangent, and more. In the interactive mode, you can test some of them. Here's the cosine of 90:


The sine of 9:


Creating your own bc functions

You can also create your own functions in bc. Function definitions start with the define keyword and are enclosed with braces. Here is a simple function entered into an interactive session that returns whatever number it's given:

$ bc
define echo(n) {
  return (n);

In the same interactive session, test it out:


If statements in bc

The bc language also has a variety of control statements, the simplest of which is if/else. The syntax may appear familiar at first glance, but there are subtleties in how braces are handled. Note that the else clause of an if-statement is enclosed in braces, while the then clause is not, but both are terminated with a semicolon. Here's a function to find the absolute value of a number n:

define abso(n) {
  if ( n > 0 ) return (n);
  { return (-n); }

In the same interactive session, test it out:


Importing data into bc

Working in an interactive session is tolerable for quick calculations and experimentation, but you lose your data when you quit, and it's difficult to edit when you make mistakes. Fortunately, bc can load variables and functions from external files.

Here's a file containing two variables (sol and foo) and a custom abso function to find an absolute value:



define abso(n) {
  if ( n > 0 ) return (n);
  { return (-n); }

Save this into a file called bcvars.bc, so you can import it into a bc interactive session:

$ bc bcvars.bc

Power-up your math with bc

The bc language is relatively simple, provided you know enough math to construct the equation for whatever you're trying to accomplish. While bc by default provides useful basic functions and allows you to create your own, you can reduce your workload by standing on the shoulders of giants. Files loaded with new functions, both for mathematic fundamentals as well as for specific tasks (for instance, calculating compound interest), are available from GNU bc page, and full documentation for bc is available.

If you're interested in doing better math in a shell, try bc. It won't turn you into a mathematics genius, but it just might make it easier to become one.

What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.


Thanks for the article. bc is a rather curious but useful application, and is capable of being used to create complicated algorithms calling multiple external functions.

I have a few notes on "if" statements, but limited to GNU bc, which is the version that most people are likely to encounter:
* The "if-else" structure is actually familiar, with its treatment of braces being identical to that used in awk.
* Semicolons are optional if newlines are used to terminate statements, just like in awk.

To demonstrate the point, your "abso(x)" function could be written as follows in GNU bc:

define abso(x) {
auto y
if (x >= 0) {
y = x
} else {
y = -x
return y

The use of the local variable "y" above is purely to demonstrate the if-else structure, with the value of y being returned at the end of the function.

I hope this comment is useful. The GNU bc manual is also well worth a read, for those who might be interested in finding out more.

To use the same shell without a pipe:

Very thanks for the article. I want to translate it to adapt in to Spanish in my web. I will obviously mention the original source. I would like your permission. Thanks!

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.