Module-2 How to do arithmetic operations in bash using the integers and float

Table of Contents

Introduction

Wondering how to do arithmetic operations in Bash scripts for integers and float like variables ? 

This article covers those in detail with examples 

How Arithmetic Expansion works for bash integer variables

We can do Arithmetic calculations in bash using the Arithmetic expansion concept for the integers , float point is not supported on the bash directly we need other tools like Bc to do the calculations 

Syntax for the arithmetic expansion in bash is as follows 

$((expression)) 

In this example we will be performing an addition using the integer dynamically typed variables

[root@discoveringsystems Bash-Scripting]# cat variable_test.sh
#!/bin/bash

Number=1

value=$((Number+Number))
echo $value
[root@discoveringsystems Bash-Scripting]# sh variable_test.sh
2

Similarly we can do a Multiplication using the variables in the bash scripts 

[root@discoveringsystems Bash-Scripting]# cat variable_test.sh
#!/bin/bash

Number=5

value=$((Number * Number))
echo $value
[root@discoveringsystems Bash-Scripting]# sh variable_test.sh
25

How to use floats instead of Integers to do arithmetic calculations in Bash ?

Bash only supports integer arithmetic natively , so we need to use other utilities like “bc” to do arithmetic operations dealing with float-like variables . So while creating the variables in Bash it takes it as string like variable and we pass them to another utility to do the calculations 

If we are dealing with the float like variables directly in Bash using the arithmetic expansion , it can end up in errors as bash does not support float arithmetic operations 

# Script failed due to the reason we were trying to use float like variables 

[root@discoveringsystems Bash-Scripting]# cat variable_test.sh
#!/bin/bash

Number=1.5

value=$((Number * Number))
echo $value

[root@discoveringsystems Bash-Scripting]# sh variable_test.sh
variable_test.sh: line 5: 1.5: syntax error: invalid arithmetic operator (error token is ".5")

Using bc tool with -l (mathlib) option  to perform similar arithmetic operations in Bash for float like variables

In the following example i am trying to do multiplication using a float like variables by piping the string expression to the bc -l tool to produce the results 

[root@discoveringsystems Bash-Scripting]# cat variable_test.sh
#!/bin/bash

Number=5.5

echo "$Number * $Number" |bc -l

[root@discoveringsystems Bash-Scripting]# sh variable_test.sh
30.25

In the following example i am trying to do division by piping the expression to the bc -l tool to produce the results which is a float like value 


[root@discoveringsystems Bash-Scripting]# cat variable_test.sh
#!/bin/bash

Number=10

echo "$Number / 3" |bc -l

[root@discoveringsystems Bash-Scripting]# sh variable_test.sh
3.33333333333333333333

In this example i am trying to do division using a float like variables by piping the expression to the bc -l tool to produce the results 

[root@discoveringsystems Bash-Scripting]# cat variable_test.sh
#!/bin/bash

Number=10.4

echo "$Number / 3" |bc -l

[root@discoveringsystems Bash-Scripting]# sh variable_test.sh
3.46666666666666666666

Since we are using the Mathlib option along with the Bc tool , the following expression which is trying to calculate the cosine of 10 also works. 

[root@discoveringsystems Bash-Scripting]# cat bash_script.sh
#!/bin/bash

Number=10

echo "c($Number)" | bc -l


[root@discoveringsystems Bash-Scripting]#  ./bash_script.sh
-.83907152907645245225

You can also perform many other complex expressions using the bc tool. Please take a look at the bc man page ( man bc) to understand various things we can do using the bc tool 

Leave a Comment

Your email address will not be published. Required fields are marked *