6. (Very) Brief intro to bc

From time to time it is useful to be able to perform simple math calculations on the command line, in bash scripts (relatively short, noncompiled programs) or as a standalone utility. The builtin math utility bc (“basic calculator”) fits the bill and can perform these calculations with arbitrary precision. It is easy to use and powerful. You need not know all its features to find it useful. We will learn a useful fraction of its capabilities.

Let’s start by using bc in interactive mode. From the command line enter:

% bc -l

By the way, to bail out of bc just type control-d and hit enter.

The -l switch above loads the math library, giving you access to a limited number of predefined math functions, and sets the default number of digits after the decimal to be 20. The latter, pre-defined variable scale can be set to whatever you please. For example,

% scale = 30

will give 30 digits after the decimal. Common math functions like addition, subtraction, multiplication and division are executed in a simple fashion:

% 5*55
% 120 - 37
% 5/2

Try these commands! Do they give you what you anticipate?

A limited number of fancier functions are pre-defined. (There is provision to define your own functions but we won’t cover that here.) These functions include the cosine, c(x), where x is the angle in radians for any trig function, the sine, s(x), and the arctangent, a(x). The natural logarithm, l(x), and the exponential function , e(x), are also defined. Note the lower case letters. Try them!

% scale = 5
% c(0)
% a(0)
% A(0)
% l(8)
% e(1)
% l(e(1))

How did things go? Was there anything peculiar about the last result? What did you expect? bc can also manipulate logical expressions, where the digit 0 represents false and any digit greater than or equal to 1 is considered true. Try these:

% 2 < 3
% 2 > 3

You can combine things with the logical or operator || and the logical and operator &&.

% 2 < 3 || 2 > 3
% 2 < 3 && 2 > 3
% 2 || 12

So far so good?

Instead of just processing numbers, matters are more interesting if we use variables. A variable is defined by just typing it. Its default value is set to 0.

% blue
% ++ blue
% ++ blue
% -- blue
% red = sqrt(16)

Note the use of the increment ++ and decrement – operators. Does the last result make sense? Now try

% blue && 12
% red || blue

Here we see the use of the logical and operator && and the logical or operator ||.

bc has 4 pre-defined variables: scale, last, ibase and obase. You’ve already seen scale. last is the value of the last number printed on the command line. The input base for calculations is set by assigning a value to ibase. The output base is specified by assigning a value to obase. The standard bases (decimal, binary, octal and hexadecimal) are included plus others up to some large integer (999?). Try these commands:

% scale = 5
% obase = 2
% 10
% ibase = 2
% obase = 10
% 10
% 4
% A
% ibase = 1010
% 4

Everything OK? Make sure you understand why ibase = 1010 was needed. We won’t talk about the control structures available in bc or other advabced features of this nifty utility. See the man pages.

bc can also be used on the command line which increases its utility. An example showing the basic structure is:

% echo ' 4^4' | bc -l
% echo 'scale = 35; 47/32' | bc -l
% echo 'scale = 10; obase = 2; sqrt(5)' | bc -l
% echo 'scale=50; 2*a(1)' | bc -l

There are several new features here. The first is the use of the echo command. echo merely prints out to the command line what follows it. What follows it can be text or the output of a command. Try this:

% echo stuff
% echo cat
% echo stuff; echo cat

The semicolon separator ; allows you to place more than 1 command on the command line at a time. It is useful for making commands compact. Note the use of the single quotation marks ‘. The pair of single quotation marks tells the shell to interpret literally what’s enclosed between them.

% echo 'stuff ; cat'
% echo stuff ; cat

See the result of leaving them off? Using them as above has the effect of piping 2 commands into bc -l but doing so on a single line.

bc can also act on text files that contain operators and functions known to it. Here is an example of a text file that contains commands digestible by bc.

scale=2


# This is a comment marker. Stuff to the right is ignored.
/* C-style comments
  are allowed, as are spaces */

# If you do not know C, DO NOT FRET !!

# print is  a predefined function.
# Words needs to be enclosed by a pair of double quotes
# \n is equivalent to hitting the Enter key.

print "\nConvert Fahrenheit to Celsius\n\n"

# read() is a predefined bc function>

print "Temperature in Fahrenheit: " ; fahr = read()
print "\n"
print "Equivalent Temperature in Celsius is: "
(fahr - 32.0) * 5.0 / 9.0
quit

# quit is another predefined function.

This file, f2c.bc, lives in my home area. Execute it by typing:

% bc -l f2c.bc

How about that? (i have edited the file in my home area so it may be slightly different from what is above.)

Previous topic

5. “Dot” files and key shell variables

Next topic

7. Flirting with bash scripting

This Page