Episode 025 – bc

Math from the Linux command line is one of those tasks that is not as straight forward as you may think. There are many tools that will allow you to perform mathematical functions accessible to you, but to perform simple arithmetic is not as simple as just entering some equation. You can use the echo command to perform basic mathematical problems but it does not allow for decimals making division in particular problematic. But if you want to simply add, subtract, multiply, or divide some values it is a handy resource:

echo $((512 * 1024))

That should return the value



echo $((4/3))

Returns the value of:


When it should be closer to:


If you are going to use echo to perform simple arithmetic you need to follow the syntax:

echo $(( number symbol number))

Similarly the expr command can be used for simple arithmetic:

expr 4 + 7

Returns the result


But again, expr will not work well with decimals:

expr 4 \/ 3



Note in this example the “/” had to be escaped.

To perform more complex mathematical operations from the command line use the bc tool. GNU bc is “an arbitrary precision calculator language” that will accept input from files, redirection or will run it’s own interactive session. Issuing bc by itself puts you into interactive mode:

screen shot 1

interactive bc session

interactive bc session

This is the same as executing bc with the -i, or –interactive, switch. At this point you can enter the statement:


Which will return


We seem to be in the same predicament as before, no decimals. This is because by default bc is operating with a scale of 0. Scale is a special variable in bc that determines the total number of decimal digits after the decimal point. To see the current value of scale while in bc execute:


The default return should be


You can alter scale like so:


Which sets scale to 3 decimal digits after the decimal point. So now:




To exit bc simply type quit.

There are a few other switches to the bc tool. The -q, –quiet, switch does not print the welcome message when entering into interactive mode. The -v, or –version, switch will show the version and copyright information for bc and then quit.

The -l, –mathlib, will load the standard math library when bc is executed. This sets the default scale to 20 and allows for additional math functions:

  • sine – s(x), x in radians
  • cosine – c(x), x in radians
  • arctangent – a(x), arctangent returns in radians
  • logarithm – l(x)
  • exponential function – e(x)
  • bessel – j(n,x)

Aside from evoking bc interactively, bc can operate on redirection:

echo 4/3 | bc



Again, we are missing the decimal point. There are to ways to resolve this:

echo 4/3 | bc -l
echo “scale=3; 4/3” | bc

Which return respecitvely


Recall that the -l switch loads the math library in and sets scale=20 by default, so there are 20 decimal digits after the decimal point. The second example we set the scale variable to 3 and thus returns 3 decimal digits after the decimal point.

There are three other special variables that alter the behaviour of bc:

  • ibase = Set the base value of the input numbers, default 10 (decimal)
  • obase = Set the base value of the output numbers, default 10 (decimal)
  • last = Is set to the value of the last number printed

Both obase and ibase are set to decimal value by default. To switch to output in binary:


To switch to input in Octal:


Note that changing this values will set the ibase and obase for all subsequent statements. To restore back to defaults either set each back to decimal:


Or quit and restart bc. Issuing these as a redirection to bc will only set those values for that session:

echo “obase=2; 5*5; obase=10; last” | bc

Will return:


The standard order of operations applies to the bc tool and can be altered with the use of parenthesis:

echo “5+2/2” |bc -l
echo “(5+2)/2” |bc -l



The bc tool has it’s own language called bc. You can do far more than just calculator functions. You can define your own functions with the define command. For example:

define litsconverter (a) {
print “binary value is “;a*1; print “\n”;
print “octal value is “; a*1; print “\n”;
print “hex value is “; a*1; print “\n”;
print “all finished (p.s.: Ignore the 0 it returns at the end of every function if a return is not used”;

Now running:


Will convert 15 (or whatever number you put in there) to binary, octal, and hex values the returns obase to decimal. The litsconverter function will remain available until you quit bc. Also note, that if we did not set obase back to decimal it would have stayed at hexidecimal until changed later on or you exited bc.

There is a lot more functionality to bc than just listed here. Consult the man page to get started on how to create your own functions to unlock the power of bc.


If the video is not clear enough view it off the YouTube website and select size 2 or full screen.  Or download the video in Ogg Theora format:

Thank you very much!

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Episode 025 – bc

Leave a Reply

Your email address will not be published.