The shell scripting language provides only the most basic mathematical operations on integer values. In most cases, this is sufficient. However, sometimes you will need to exceed those limitations to perform more complicated mathematical operations.
There are two main ways to do floating point math (and other, more sophisticated math). The first is through the use of inline Perl code, the second is through the use of the bc command. This section presents both forms briefly.
Floating Point Math Using Inline Perl
Floating Point Math Using the bc Command
The first method of doing shell floating point math, inline Perl, is the easiest to grasp. To use this method, you essentially write a short perl script, then substitute shell variables into the script, then pass it to the perl interpreter, either by writing it to a file or by passing it in as a command line argument.
Note: Length limitations apply when passing in a Perl script by way of a command line argument. The exact limitations vary from one OS to another, but are generally in the tens of kilobytes. If your script needs to be longer, it should be written out to a file.
The following example demonstrates basic floating point math using inline Perl. It assumes a basic understanding of the Perl programming language.
#!/bin/sh |
PI=3.141592654 |
RAD=7 |
AREA=$(perl -e "print \"The value is \".($PI * ($RAD*$RAD)).\"\n\";") |
echo $AREA |
Under normal circumstances, you probably would not want to print an entire string when doing this. However, the use of the string was to demonstrate an important point. Perl evaluates strings between single and double quote marks differently, so when doing inline Perl, it is often necessary to use double quotes. However, the shell only evaluates shell variables within double quotes. Thus, the double quote marks in the script had to be quoted so that they would actually get passed to the Perl interpreter instead of ending or beginning new command-line arguments.
This need for quoting can prove to be a challenge for more complex inline code, particularly when regular expressions is involved. In particular, it can often be tricky figuring out how many backslashes to use when quoting the quoting of a quotation mark within a regular expression. Such issues are beyond the scope of this document, however.
The bc command, short for basic calculator, is a POSIX command for doing various mathematical operations. The bc command offers arbitrary precision floating point math, along with a built-in library of common mathematical functions to make programming easier.
Note: The most common version of bc (and the one included in Mac OS X) is GNU bc, which offers a number of extensions beyond those available in the POSIX version. For cross-platform compatibility, you should generally avoid these extensions if possible. If you specify the -s flag to GNU bc, it will disable the GNU extensions and will thus emulate the POSIX version.
The bc command takes its input from its standard input, not from the command line. If you pass it command line arguments, they are interpreted as file names to be executed, which is probably not what you want to do when executing math operations inline in a shells script.
Here is an example of using bc in a shell script:
#!/bin/sh |
PI=3.141592654 |
RAD=7 |
AREA=$(echo "$PI * ($RAD ^ 2)" | bc) |
echo "The area is $AREA" |
The bc command offers much more functionality than described in this section. This section is only intended as a brief synopsis of the available functionality. For full usage notes, see the man page for bc.
Last updated: 2008-04-08