Shell Scripting: Floating-point numbers

Samuel Kadima
3 min readMay 9, 2023

--

Shell scripting is a powerful tool for automation, and it can handle mathematical calculations with ease. However, by default, shell scripting only works with integers, which can be limiting in some cases.

In this article, we will explore the use of floating-point numbers in shell scripting, and how to perform arithmetic operations with them. We will also provide practical examples to showcase the usefulness of floating-point numbers in shell scripting.

Steps

Navigate to your working directory or create a new directory using the following command and navigate to it

mkdir shell_scripting && cd shell_scripting

Create a file in this directory with the name float.sh . Do so using the following command

touch float.sh

Make the file executable using the following command

chmod +x float.sh

Open the file with nano as shown below

nano float.sh

Add the following line at the top of the file. This will allow this file to be executed using bash

#! /usr/bin/bash

In shell scripting, floating point numbers are decimal numbers used to represent real numbers with a fractional part. They are represented as double-precision numbers and are stored as 64-bit values in memory.

Create two variables to store two floating point numbers as shown below

#!/usr/bin/bash
NUM1=12.5
NUM2=6.0

NUM1 and NUM2 are variables that store our floating point numbers.

Lets write a script to add the two numbers as we did in the arithmetic operations article

Add the following to the script

#!/usr/bin/bash
NUM1=12.5
NUM2=6.0

echo $(( NUM1 + NUM2 ))

Save the file and exit the nano using the following commands: Ctrl + o then Enter then Ctrl + x

Run your script as shown below

~$ ./float.sh

You should see the following error on your terminal

./float.sh: line 5: 12.5: syntax error: invalid arithmetic operator (error token is ".5")

This confirms that the shell only supports integer operations by default.

In order to perform this kind of operations, we will use the basic calculator(bc) which is a command-line utility used for performing mathematical calculations in shell scripting. It can handle floating-point arithmetic, as well as more complex mathematical operations such as trigonometric functions and logarithms.

Let us alter our script to use bc as shown below

#!/usr/bin/bash
NUM1=12.5
NUM2=6.0

echo "$NUM1 + $NUM2" | bc

Now if we save and execute our file we get the expected output

~$ ./float.sh
18.5

We can try out other arithmetic operations using bc

Edit your script and add the following

#!/usr/bin/bash
NUM1=12.5
NUM2=6.0
SCALE=2

echo "The sum of floating-point numbers is: $(echo "$NUM1 + $NUM2" | bc -l)"
echo "The difference of floating-point numbers is: $(echo "$NUM1 - $NUM2" | bc -l)"
echo "The product of floating-point numbers is: $(echo "$NUM1 * $NUM2" | bc -l)"
echo "The quotient of floating-point numbers is: $(echo "scale=$SCALE; $NUM1 / $NUM2" | bc -l)"

SCALE=2 allows us to limit our decimal numbers to 2

-l allows us to use the math module for our operation

Save and run your script. Everything is now working fine.

I challenge you to recreate the calculator we created in the arithmetic operators article while accounting for floating point numbers

Congratulations!

If you like this article, kindly follow for more.

--

--