Bash Scripting: Arithmetic operations

The need to perform basic arithmetic operations is common in all types of programming, including in Bash scripts. A Linux system has multiple ways to perform arithmetic operations, and it is up to the user to decide the best method for the scenario at hand.

In this tutorial, you will learn several ways to use arithmetic operations to perform basic calculations inside of a Bash script on Linux. Check out the examples below to see how these different methods work.

In this tutorial you will learn:

  • How to use arithmetic operations with double parentheses, bc, let, expr, and awk in a Bash script
Example of performing arithmetic operations in a Bash script on Linux
Example of performing arithmetic operations in a Bash script on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash Scripting: Arithmetic operations examples




The methods covered below are double parentheses (( )), basic calculator bc, let, expr, and awk commands.

All should do the job, so use whichever one makes the most sense for your scripting environment.

Double parentheses

While the other methods rely on a command to perform an arithmetic operation, the double parentheses method is integrated in the Bash shell and is a great choice for performing calculations with numbers and/or variables inside of a Bash script. Let’s look at an example below to see different ways this method can be used.

#!/bin/bash

# Perform basic arithmetic with numbers (add, subtract, multiply, divide)
fig1=$((100-50*2/3))
echo $fig1

# Increment a variable
((fig2 = 3))
((fig2++))
echo $fig2

# Decrement a variable
((fig3 = 3))
((fig3--))
echo $fig3

# Using shorthand operator to add
((fig4 = 10))
((fig4 += 10))
echo $fig4

And here is the result when we execute the script:

$ ./double_parentheses.sh
67
4
2
20

Basic calculator (bc command)

If you are familiar with the C programming language, you will probably notice that the syntax for the bc command is quite similar. The calculator can also handle variables and algebra, or do other useful things like convert numbers to hexadecimal. Let’s look at how to use this command to perform arithmetic calculations in the example.

#!/bin/bash

# Perform basic arithmetic with numbers (add, subtract, multiply, divide)
echo "100-50*2/3" | bc

# Return integer only
echo "11/3" | bc

# Return floating point decimal by using -l option
echo "11/3" | bc -l

# Use shorthand operator to perform addition
echo "x=1; x+=2;x" | bc

And here is the result when we execute the script:

$ ./basic_calculator.sh
67
3
3.66666666666666666666
3

Let command




The let command accepts very simple syntax, making it easy to use. Here is how to use the let command to do various arithmetic operations in a Bash script.

#!/bin/bash

# bash addition
let ADDITION=3+5
echo "3 + 5 =" $ADDITION

# bash subtraction
let SUBTRACTION=7-8
echo "7 - 8 =" $SUBTRACTION 

# bash multiplication
let MULTIPLICATION=5*8
echo "5 * 8 =" $MULTIPLICATION

# bash division
let DIVISION=4/2
echo "4 / 2 =" $DIVISION

# bash modulus
let MODULUS=9%4
echo "9 % 4 =" $MODULUS

# bash power of two
let POWEROFTWO=2**2
echo "2 ^ 2 =" $POWEROFTWO

And here is the result when we execute the script:

$ ./let_command.sh
3 + 5 = 8
7 - 8 = -1
5 * 8 = 40
4 / 2 = 2
9 % 4 = 1
2 ^ 2 = 4

expr command

An older method for Bash arithmetic operations is the expr command. This accepts a simple syntax like the let command, but is picky about spacing. Keep in mind that expr will only work with integers so you must use a different method if you are working with decimals.

#!/bin/bash

# Addition and subtraction
expr 50 + 50
expr 100 - 50

# Multiplication and division
expr 3 \* 12
expr 12 / 3

# Calculate the remainder
expr 11 % 3

And here is the result when we execute the script:

$ ./expr_command.sh
100
50
36
4
2


awk command

The awk command on Linux can be used to do many things, and it is worth everyone’s time to master it or at least become very familiar with it. One of its many functions includes being used as a calculator. Here is how to use the awk command to perform various types of arithmetic operations in a Bash script.

#!/bin/bash

# Addition with awk
echo | awk '{ print 100 + 50 }'

# Subtraction with awk
echo | awk '{ print 100 - 50 }'

# Multiplication with awk
echo | awk '{ print 100 * 50 }'

# Division with awk
echo | awk '{ print 100 / 50 }'

# Floating decimal with awk
echo | awk '{ print 11 / 3 }'

# Calculate the square root of 5
echo 5 | awk '{ print sqrt($root) }'

And here is the result when we execute the script:

$ ./awk.sh
150
50
5000
2
3.66667
2.23607

Closing Thoughts

In this tutorial, we saw how to perform arithmetic operations in a Bash script on Linux. The Bash shell gives us many different ways to perform these calculations. Each method has its pros and cons, and one might fit your exact scenario better than others. Feel free to copy any of our examples above and adapt them as needed in your own Bash scripts.



Comments and Discussions
Linux Forum