What is a good command-line calculator on Linux

Every modern Linux desktop distribution comes with a default GUI-based calculator app. On the other hand, if your workspace is full of terminal windows, and you would rather crunch some numbers within one of those terminals quickly, you are probably looking for a command-line calculator. In this category, GNU bc (short for "basic calculator") is a hard to beat one. While there are many command-line calculators available on Linux, I think GNU bc is hands-down the most powerful and useful.

Predating the GNU era, bc is actually a historically famous arbitrary precision calculator language, with its first implementation dating back to the old Unix days in 1970s. Initially bc was a better known as a programming language whose syntax is similar to C language. Over time the original bc evolved into POSIX bc, and then finally GNU bc of today.

Features of GNU bc

Today's GNU bc is a result of many enhancements of earlier implementations of bc, and now it comes standard on all major GNU/Linux distros. It supports standard arithmetic operators with arbitrary precision numbers, and multiple numeric base (e.g., binary, decimal hexadecimal) of input and output.

If you are familiar with C language, you will see that the same or similar mathematical operators are used in bc. Some of supported operators include arithmetic (+,-,*,/,%,++,--), comparison (<,>,==,!=,<=,>=), logical (!,&&,||), bitwise (&,|,^,~,<<,>>), compound assignment (+=,-=,*=,/=,%=,&=,|=,^=,&&=,||=,<<=,>>=) operators. bc comes with many useful built-in functions such as square root, sine, cosine, arctangent, natural logarithm, exponential, etc.

How to Use GNU bc

As a command-line calculator, possible use cases of GNU bc are virtually limitless. In this tutorial, I am going to describe a few popular features of bc command. For a complete manual, refer to the official source.

Unless you have a pre-written bc script, you typically run bc in interactive mode, where any typed statement or expression terminated with a newline is interpreted and executed on the spot. Simply type the following to enter an interactive bc session. To quit a session, type 'quit' and press Enter.

$ bc

The examples presented in the rest of the tutorial are supposed to be typed inside a bc session.

Type expressions

To calculate an arithmatic expression, simply type the expression at the blinking cursor, and press Enter. If you want, you can store an intermediate result to a variable, then access the variable in other expressions.

Within a given session, bc maintains a unlimited history of previously typed lines. Simply use UP arrow key to retrieve previously typed lines. If you want to limit the number of lines to keep in the history, assign that number to a special variable named history. By default the variable is set to -1, meaning "unlimited."

Switch input/output base

Often times you want to type input expressions and display results in binary or hexadecimal formats. For that, bc allows you switch the numeric base of input or output numbers. Input and output bases are stored in ibase and obase, respectively. The default value of these special variables is 10, and valid values are 2 through 16 (or the value of BC_BASE_MAX environment variable in case of obase). To switch numeric base, all you have to do is to change the values of ibase and obase. For example, here are examples of summing up two hexadecimal/binary numbers:

Note that I specify obase=16 before ibase=16, not vice versa. That is because if I specified ibase=16 first, the subsequent obase=16 statement would be interpreted as assigning 16 in base 16 to obase (i.e., 22 in decimal), which is not what we want.

Adjust precision

In bc, the precision of numbers is stored in a special variable named scale. This variable represents the number of decimal digits after the decimal point. By default, scale is set to 0, which means that all numbers and results and truncated/stored in integers. To adjust the default precision, all you have to do is to change the value of scale variable.

scale=4

Use built-in functions

Beyond simple arithmatic operations, GNU bc offers a wide range of advanced mathematical functions built-in, via an external math library. To use those functions, launch bc with "-l" option from the command line.

Some of these built-in functions are illustrated here.

Square root of N:

sqrt(N)

Sine of X (X is in radians):

s(X)

Cosine of X (X is in radian):

c(X)

Arctangent of X (The returned value is in radian):

a(X)

Natural logarithm of X:

l(X)

Exponential function of X:

e(X)

Other goodies as a language

As a full-blow calculator language, GNU bc supports simple statements (e.g., variable assignment, break, return), compound statements (e.g., if, while, for loop), and custom function definitions. I am not going to cover the details of these features, but you can easily learn how to use them from the official manual. Here is a very simple function definition example:

define dummy(x){
return(x * x);
}
dummy(9)
81
dummy(4)
16

Use GNU bc Non-interactively

So far we have used bc within an interactive session. However, quite popular use cases of bc in fact involve running bc within a shell script non-interactively. In this case, you can send input to bc using echo through a pipe. For example:

$ echo "40*5" | bc
$ echo "scale=4; 10/3" | bc
$ echo "obase=16; ibase=2; 11101101101100010" | bc

To conclude, GNU bc is a powerful and versatile command-line calculator that really lives up to your expectation. Preloaded on all modern Linux distributions, bc can make your number crunching tasks much easy to handle without leaving your terminals. For that, GNU bc should definitely be in your productivity toolset.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

11 thoughts on “What is a good command-line calculator on Linux

  1. I like to use irb, a utility of the Ruby language. It is simple to do simple or complex calculations with all the operations of Ruby.

  2. I usually use bc if I am in command mode and need just quick addition. Python is good alternative, as mentioned. But since even in command line I am normally in an x terminal, I usually just thunk a browser to wolframalpha or use Frink, so I get natural answers including units, which helps me recognize when I phrased the math wrong.

  3. Others to look at are szg, calc, and genius (the latter extends normal calculator idea to things like summing definite series, numerical integration, and LaTeX and MathML output.) There is also maxima, which is a pretty full featured symbolic algebra and calculus system, but has a definite learning curve.

  4. At times, I find that I need additional power, and the Python REPL provides many of those features. I just type "python" at the command-line and then have all the power of Python at my disposal, including arrays, dictionaries, reading data from a file, etc. When done, I just use control+D (or control+Z on Win32) to provide an EOF and quit. Other languages such as Ruby ("irb"), Perl ("perl -d -e 1"), and Lua ("lua") provide similar REPLs if their flavors of expression better match your way of thinking.

    • I think the advantage of a program like bc is that you can hand it to an untrusted user and not worry. In the scripting example handing off arbitrary input from the command line to a full-fledged REPL may not be the best idea in the world security wise.

  5. When I want to do quick and dirty calculations in a terminal, I invoke a perl script I wrote a long time ago that resides in my path. I named it calc. Nothing fancy, but does simple math, which is basically what I use it for.

    #!/usr/bin/perl
    print eval(shift @ARGV), "\n";

  6. I'm using the following custom function defined in my .bash_aliases:
    function ? {
    awk "BEGIN{ pi = 4.0*atan2(1.0,1.0); deg = pi/180.0; print $@ }"
    }
    usage:
    $ ? 100/4*pi
    78.5398

    $ ? "34.34/(pi*4)"
    2.73269

    $ ? "sin(45*deg)"
    0.707107

Leave a comment

Your email address will not be published. Required fields are marked *