# Floating Point Arithmetic in the Bourne Again Shell (BASH)

This is a brief post on quick ways to perform floating point arithmetic in the GNU Bourne Again Shell (BASH), either at the command prompt or in a shell script. It is partially a followup to the post Ten Great Quick Calculators for Computer Users and overlaps some of the content in this previous post.

The Bourne Again Shell, or bash, is the default command line processor (shell) for Mac OS X, the cygwin environment on MS Windows, and many other Unix and Unix-like systems. Especially if you work with numbers, you may want to perform quick floating point arithmetic at the command prompt (shell) or in shell scripts. This post covers five ways to perform floating point arithmetic in bash: the bc arbitrary precision calculator language, the awk pattern scanning and processing language, the perl programming language, the python programming language, and the ruby programming language.

Integer Only Arithmetic in BASH

Note: One can perform integer arithmetic and integer arithmetic alone (no floating point) by using the Unix expr utility and the bash \$[N op M] syntax where N and M are any integers and op is any arithmetic operator (+, -, *, /). For example:

```[email protected] ~
\$ expr 1 + 2
3

[email protected] ~
\$ echo \$[1 + 2]
3
```

These integer-only arithmetic methods were described more fully in the previous post Ten Great Quick Calculators for Computer Users.

Five Ways to Perform Floating Point Arithmetic in BASH

(1) BC

The following commands show how to perform floating point arithmetic (addition, subtraction, multiplication, and division) at the bash shell prompt using the bc arbitrary precision calculator language. Note the need to escape the multiply operator * with a backslash or enclose the arithmetic expression in single quotes. Note the use of the backtick myvar=`command` syntax to assign the results of the arithmetic to a bash variable.

Note that there should be no space between the variable name and the equal sign in the assignment, otherwise an error occurs.

```[email protected] ~
\$ echo 1.1 + 2.2 | bc -l
3.3

[email protected] ~
\$ echo 1.1 - 2.2 | bc -l
-1.1

[email protected] ~
\$ echo 1.1 * 2.2 | bc -l
1.1  (THIS IS WRONG!!!!)

[email protected] ~
\$ echo 1.1 \* 2.2 | bc -l
2.42   (THIS IS RIGHT!!!)

[email protected] ~
\$ echo '1.1 * 2.2' | bc -l
2.42    (THIS IS RIGHT!!!)

[email protected] ~
\$ echo '1.1 / 2.2' | bc -l
.50000000000000000000

[email protected] ~
\$ myvar=`echo '1.1 / 2.2' | bc -l`

[email protected] ~
\$ echo \$myvar
.50000000000000000000

```

(2) AWK

The following commands show how to perform floating point arithmetic (addition, subtraction, multiplication, and division) at the bash prompt using the awk pattern scanning and processing language. The AWK operators and functions for raising a number to a power (**,^), the square root (sqrt(x)), the natural logarithm (log(x)), the sine function (sin(x)), cosine function (cos(x)), and the arctangent (atan2(y,x)) are also shown. Note the use of the backtick myvar=`command` syntax to assign the results of the arithmetic to a bash variable. AWK has the advantage that it is very old and is almost always available on Unix and Unix-like systems.

Note that there should be no space between the variable name and the equal sign in the variable assignment.

```[email protected] ~
\$ echo - | awk '{print 1.1 + 2.2}'
3.3

[email protected] ~
\$ echo - | awk '{print 1.1 - 2.2}'
-1.1

[email protected] ~
\$ echo - | awk '{print 1.1 * 2.2}'
2.42

[email protected] ~
\$ echo - | awk '{print 1.1 / 2.2}'
0.5

[email protected] ~
\$ echo - | awk '{print 1.1 ^ 2.2}'
1.23329

[email protected] ~
\$ echo - | awk '{print 1.1 ** 2.2}'
1.23329

[email protected] ~
\$ echo - | awk '{print log(10.0)}'
2.30259

[email protected] ~
\$ echo - | awk '{print sqrt(2.0)}'
1.41421

[email protected] ~
\$ echo - | awk '{print sin(1.0)}'
0.841471

[email protected] ~
\$ echo - | awk '{print cos(1.0)}'
0.540302

[email protected] ~
\$ echo - | awk '{print atan2(1.0, 1.0)}'
0.785398

[email protected] ~
\$ myvar=`echo - | awk '{print 1.1 + 2.2}'`

[email protected] ~
\$ echo \$myvar
3.3

```

(3) PERL

The following commands show how to perform floating point arithmetic (addition, subtraction, multiplication, and division) using the Perl programming language at the bash prompt. Note the use of the backtick myvar=`command` syntax to assign the results of the arithmetic to a bash variable. Perl is widely used and almost always already installed on Unix and Unix-like systems.

Note that there should be no space between the variable name and the equal sign in the variable assignment.

```[email protected] ~
\$ perl -e 'print 1.1 + 2.2'
3.3
[email protected] ~
\$ perl -e 'print 1.1 - 2.2'
-1.1
[email protected] ~
\$ perl -e 'print 1.1 * 2.2'
2.42
[email protected] ~
\$ perl -e 'print 1.1 / 2.2'
0.5
[email protected] ~
\$ myvar2=`perl -e 'print 1.1 + 2.2'`

[email protected] ~
\$ echo \$myvar2
3.3

```

(4) PYTHON

The following commands show how to perform floating point arithmetic (addition, subtraction, multiplication, and division) using the Python programming language at the bash prompt. Note the use of the backtick myvar=`command` syntax to assign the results of the arithmetic to a bash variable. Python is widely used and frequently already installed on Unix and Unix-like systems.

Note that there should be no space between the variable name and the equal sign in the variable assignment.

```[email protected] ~
\$ python -c 'print 1.1 + 2.2'
3.3

[email protected] ~
\$ python -c 'print 1.1 - 2.2'
-1.1

[email protected] ~
\$ python -c 'print 1.1 * 2.2'
2.42

[email protected] ~
\$ python -c 'print 1.1 / 2.2'
0.5

[email protected] ~
\$ myvar3=`python -c 'print 1.1 + 2.2'`

[email protected] ~
\$ echo \$myvar3
3.3
```

(5) RUBY

The following commands show how to perform floating point arithmetic (addition, subtraction, multiplication, and division) using the Ruby programming language at the bash prompt. Note the use of the backtick myvar=`command` syntax to assign the results of the arithmetic to a bash variable. Ruby is widely used and sometimes already installed on Unix and Unix-like systems.

Note that there should be no space between the variable name and the equal sign in the variable assignment.

```[email protected] ~
\$ ruby -e 'print 1.1 + 2.2'
3.3
[email protected] ~
\$ ruby -e 'print 1.1 - 2.2'
-1.1
[email protected] ~
\$ ruby -e 'print 1.1 * 2.2'
2.42
[email protected] ~
\$ ruby -e 'print 1.1 / 2.2'
0.5
[email protected] ~
\$ myvar4=`ruby -e 'print 1.1 + 2.2'`

[email protected] ~
\$ echo \$myvar4
3.3

```

These same commands to perform floating point arithmetic can be included in Bourne Again Shell (bash) scripts.

Conclusion

There are at least five ways to perform floating point arithmetic under the Unix or GNU Bourne Again Shell (bash), either at the command prompt or in shell scripts. These are the bc arbitrary precision calculator language, the awk pattern scanning and processing language, the perl programming language, the python programming language, and the ruby programming language.

AWK is most likely to be preinstalled on a Unix or Unix-like system, followed by BC and PERL. Python is now fairly common although still not as common as PERL. RUBY is becoming fairly common, but it is still not as common as Python in the author’s experience. BC has the special advantage that it is an arbitrary precision calculator whereas the others are usually 32 or 64 bit precision floating point by default.

1. S July 23, 2012
2. Dave Howcroft July 24, 2012
3. alex January 28, 2015
4. Joel Parker Henderson November 9, 2015
5. WAZAAAAAAP March 22, 2017
6. Daniel January 6, 2018