Floating Point Arithmetic in Bash

Naila Saad Siddiqui Feb 15, 2024
  1. Integer Arithmetic in Bash
  2. Floating-Point Arithmetic in Bash
  3. Use the Basic Calculator (bc) for Floating-Point Arithmetic in Bash
  4. Use the awk Command for Floating-Point Arithmetic in Bash
  5. Use the perl Command for Floating-Point Arithmetic in Bash
  6. Use the python Command for Floating-Point Arithmetic in Bash
  7. Conclusion
Floating Point Arithmetic in Bash

This short article describes how to quickly perform floating point calculations in the GNU BASH (Shell Scripting), either directly at the command prompt or in a shell script.

Performing quick floating point calculations at the command prompt (shell) or in shell scripts may be helpful if you work with numbers. The following four methods to do floating-point arithmetic in Bash are covered in this article:

  1. The arbitrary precision calculator method using the bc command.
  2. Using the awk pattern scanning and processing method.
  3. Using the perl command method.
  4. Using the python command method.

Integer Arithmetic in Bash

Integer-only arithmetic can be performed easily in the Bash script using either the expr command on the command line or square brackets to evaluate your expression $[1+1]. This is shown in the code below:

#!/bin/bash
echo $[2 + 1]

This will generate the following output:

expr and integer arithmetic output

But these are integer-only calculations. If the answer is in floating-point in these calculations, then it displays its integral part only.

To perform floating-point operations, we need help from the tools discussed below.

Floating-Point Arithmetic in Bash

Multiple tools can be used to perform floating-point arithmetic operations in Bash. However, this article will explore the four most widely used and readily available (in all the UNIX or Linux OS family) tools.

Use the Basic Calculator (bc) for Floating-Point Arithmetic in Bash

For a command-line calculator, use the bc command. It is comparable to a simple calculator we can use to perform simple mathematical calculations.

The most fundamental operations in any programming language are arithmetic operations. The bc and expr commands are available in the Linux or Unix operating system for performing mathematical operations.

These commands can evaluate arithmetic expressions in shell script or Bash.

The following script will calculate the addition and division of floating-point numbers and show the screen result.

#!/bin/bash
echo "Addition: "
echo '1.5 + 2.5' | bc -l
echo "Division"
echo '2.1/3.2' | bc -l

This will give the following output:

bc output

Use the awk Command for Floating-Point Arithmetic in Bash

Real numbers or floating-point numbers contain a fractional component. All numerical values are represented by double-precision floating-point numbers in awk.

To put it another way, all numbers in awk are floating-point numbers, meaning that all calculations use these numbers.

The awk command has the benefit that it is available in all UNIX-like operating systems or Linux distributions as it is pretty old and has been in use for a long time.

The following script calculates the multiplication and division of two floating point numbers using awk.

#!/bin/bash
echo "Multiplication: "
echo - | awk '{print 2.1 * 3.2}'
echo "Division"
echo - | awk '{print 2.1 / 3.2}'

This will give the following output:

awk output

Use the perl Command for Floating-Point Arithmetic in Bash

Perl is a programming language that usually comes in a package with all Linux distributions or other UNIX-like operating systems. The perl command can be used in Bash, and it helps perform floating-point arithmetic operations in Linux.

It can perform all the operations like addition, subtraction, multiplication, division, and assignment operator.

The following script calculates the subtraction and division of two floating-point numbers using the perl command in Linux:

#!/bin/bash
echo "Subtraction: "
perl -e 'print 4.1 - 6.2'
echo "Division"
perl -e 'print 4.1 / 2.2'

This will give the following output:

perl output

Use the python Command for Floating-Point Arithmetic in Bash

Just like Perl, Python is another language used widely in all fields of programming. This is a frequently used language and is sometimes preinstalled with your Linux distribution.

The python command helps perform floating-point arithmetic operations in the Bash script. It can perform all the operations like addition, subtraction, multiplication, division, and assignment operator.

The following script will calculate the addition and division of two floating-point numbers using the python command:

#!/bin/bash
echo "Addition: "
python -c 'print 4.1 + 6.2'
echo "Division"
python -c 'print 4.1 / 2.2'

This will give the following output:

python output

Conclusion

Under Unix or the GNU Bourne Again Shell (Bash), there are at least four ways to perform floating point arithmetic at the command line or in the scripts. These are awk, perl, python, and bc commands.

A Unix system will likely come with AWK, BC, and Perl preinstalled. Though Python is still less popular than PERL, it is now fairly widespread.

Unlike other calculators, which typically default to 32 or 64-bit precision floating-point, BC has the unique advantage of being an arbitrary precision calculator.