BASH Programming

How to Subtract an Integer from a Variable – Bash

In Bash, variables are used to store data and can be of different types such as integers, strings, and arrays. Performing arithmetic operations on variables is a common requirement in shell scripting. Subtraction is a basic arithmetic operation and can be performed on variables in Bash using various methods. In this article, we will discuss how to subtract an integer from a variable in Bash and here are some ways to do it.

1: How to Subtract Integer from a Variable Using the (()) Syntax

The (( )) syntax is used for arithmetic operations in bash and to subtract an integer from a variable using this syntax, the variable name is enclosed within the double parentheses, followed by the arithmetic operator “-” and the integer to subtract. Here is a bash script example that subtract the variable in which the value of 20 is stored and subtracts the 7 from it using (()) syntax:

#!/bin/bash
num=20
echo "Original value: $num"
((num=num-7))
echo "Value after subtracting 7: $num"

 
Here is the output for the code I have given for illustration purposes and in output it can be seen that an integer is subtracted from the variable:

2: How to Subtract Integer from a Variable using the $(( )) syntax

The $(( )) syntax is also used for arithmetic operations in bash. To subtract an integer from a variable using this syntax, the variable name is enclosed within the double parentheses, preceded by the arithmetic operator “-” and the integer to subtract. To illustrate the use of this syntax i have given an example bash script that subtracts the integer from a variable:

#/bin/bash
num=20
echo "Original value: $num"
num=$((num-7))
echo "Value after subtracting 7:" $num

 
In the output an integer is subtracted from the variable:

3: How to Subtract Integer from a Variable using let Command

The let command is used for arithmetic operations in bash. To subtract an integer from a variable using this command, the variable name is followed by the arithmetic operator “-” and the integer to subtract. To illustrate the use of this command an example of bash script that subtracts the integer from a variable is given below:

#/bin/bash
num=20
echo "Original value: $num"
let "num=num-7"
echo "Value after subtracting 7: $num"

 
Here is the output for the code I have given for illustration purposes and in output an integer is subtracted from the variable:

4: How to Subtract Integer from a Variable Using the expr Command

The expr command is used for arithmetic operations in bash. To subtract an integer from a variable using this command, the variable name is followed by the arithmetic operator “-” and the integer to subtract, enclosed within the backticks. To illustrate the use of this command I have given an example bash script that subtracts the integer from a variable:

#/bin/bash
num=20
echo "Original value: $num"
num=`expr $num - 7`
echo "Value after subtracting 7:" $num

 
And in output an integer is subtracted from the variable:

5: How to Subtract Integer from a Variable Using the declare Command

The declare command is used to set attributes to variables in bash. To subtract an integer from a variable using this command, the variable name is followed by the arithmetic operator “-” and the integer to subtract, enclosed within the curly braces.

#!/bin/bash
num=20
echo "Original value: $num"
declare -i num
num=num-7
echo "Value after subtracting 7: $num"

 
In output an integer is subtracted from the variable:

Conclusion

There are five ways to subtract an integer from a variable in bash and those are: using (()) syntax, using the $(( )) syntax, using declare and let commands. The choice of method depends on personal preference and coding requirements.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.