Do math in the Linux shell with GNU bc |

Do math in the Linux shell with GNU bc

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

old school calculator
Image credits : 

Subscribe now

Get the highlights in your inbox every week.

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.

A dollar sign in a network

Manage your personal or small business accounting with GnuCash.
Tux with binary code background

If you're a heavy Linux user, you'll probably recognize these common tendencies.
Math formulas in green writing

If you're looking for scientific computing software, but can't afford the leading proprietary solution, check out these open source options.


About the author

Seth Kenlon
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. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.