FUNDAMENTALS A Complete Guide for Beginners
Subtraction is one of the most common yet important mathematical operations on Bash variables. When Bash programmers work with data sets, they do various kinds of arithmetic operations for doing necessary data manipulation tasks and taking vital information from the result. Here, in this article, I will show you different ways to subtract two bash variables.
Subtract Two Variables in the Bash Scripts
Some common approach to subtracting variables in Bash is given below:
- Straightforward approach to subtract variables in bash:
((num=num-10))
- Subtracting two bash variables using arithmetic expansion:
num=$((num-10))
- Subtracting two bash variables Using let command:
let "num=num-10"
Here it updates the variable of the current shell.
- To subtract an integer from a variable follow the below syntax using the expr command:
num=`expr $num - 10`
- To subtract one variable from another variable, follow the below syntax using the expr command:
result=`expr $num1 - $num2`
Here it updates the value in a new shell
- To convert the variable to an integer and then do the subtraction Using declare command:
declare -i num num=num-15
4 Practical Examples Related to Subtracting Two Bash Variables
The following section of this article will show you some approaches to subtracting two bash variables, such as using double bracket (()), using arithmetic expansion $(()), using the declare command, using the expr command, and using the let command. Without further delay, let’s jump into the topic.
Example 01: Subtract Two Variables
Here I have two variables named num1 and num2. I will subtract the num2 from the num1 variable utilizing the double bracket (()) and print the result on the terminal. To know more, follow the below steps.
To subtract two variables using double parentheses, follow the script mentioned below:
#!/bin/bash
#assigning num1 and num2 variable
num1=50
num2=10
#subtracting num2 from num1 variable
((num3=num1-num2))
#printing the result of subtraction
echo "Value after subtracting num2 from num1: $num3"
#!/bin/bash
, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. After that, the num1=50 num2=10
commands have assigned a value to the num1
and num2
variables. Subsequently, the ((num3=num1-num2))
command has subtracted the value of the num2
variable from the num1
variable utilizing the double bracket method. Finally, the echo "Value after subtracting num2 from num1: $num3"
command has printed the result after subtracting the num1
from the num2
variable.
The image shows that the Bash script has subtracted the num2 variable from the num1 variable and then printed the result on the terminal.
Example 02: Subtract Two Variables Using Arithmetic Expansion
Here I have two variables named num1 and num2. I will subtract the num2 from the num1 variable utilizing the arithmetic expansion $(()) and print the result on the terminal. To know more, follow the below script.
To subtract two variables in Bash using arithmetic expansion, you can use the following script:
#/bin/bash
#assigning num1 and num2 variable
num1=50
num2=10
#subtracting num2 from num1 variable
num3=$((num1-num2))
#printing the result of subtraction
echo "Value after subtracting num2 from num1: $num3"
the num1=50 num2=10
commands have assigned a value to the num1
and num2
variables. Subsequently, the ((num3=num1-num2))
command has subtracted the value of the num2
variable from the num1
variable utilizing the double bracket method. Finally, the echo "Value after subtracting num2 from num1: $num3"
command has printed the result after subtracting the num1
from the num2
variable.
The image shows that the bash script has subtracted the num2 variable from the num1 variable utilizing the arithmetic expression and printed the result on the terminal.
Example 03: Subtract Integer From a Variable Using Commands
There are some commands like expr command, let command, and declare command which we can use to subtract integers from a variable. Here, in three cases, I will show you the demonstration of subtracting and incorporating these above-mentioned commands.
Case 01: Subtract Integer From a Variable Using the “let” Command
Here I have a variable named num. I will print the original value of the num variable. Then subtract 10 from the num variable utilizing the let command and print the result on the terminal.
To subtract an integer from a variable in Bash using the let command, you can use the following script:
#/bin/bash
#assigning and printing the value of num variable
num=50
echo "Original value: $num"
#subtracting 10 from num variable
let "num=num-10"
#printing the result
echo "Value after subtracting 10: $num"
The num=50
command has assigned a value to the num variable. Afterwards, the echo "Original value: $num"
command has printed the original value of the num variable on the terminal. Subsequently, the let "num=num-10"
command has subtracted the value of the num variable by 10 utilizing the let command. Finally, the echo "Value after subtracting 10: $num"
command has printed the value after subtracting 10.
The image shows that the bash script has printed the original value of the num variable which is 50 and the value after subtracting 10 utilizing the let command which is 40 on the terminal.
Case 02: Subtract Integer From a Variable Using the “expr” Command
Here I have a variable named num. Here I will print the original value of the num variable. Then subtract 10 from the num variable utilizing the expr command and print the result on the terminal.
To subtract an integer from a variable in Bash using the expr command, use the following script:
#/bin/bash
#assigning and printing the value of num variable
num=50
echo "Original value: $num"
#subtracting 10 from num variable
num=`expr $num - 10`
#printing the result on the terminal
echo "Value after subtracting 10:" $num
The num=50
command has assigned a value to the num variable. Afterwards, the echo "Original value: $num"
command has printed the original value of the num variable on the terminal. Subsequently, the num=`expr $num - 10`
command has subtracted the value of the num variable by 10 utilizing the expr command. Finally, the echo "Value after subtracting 10: $num"
command has printed the value after subtracting 10.
The image shows that the bash script has printed the original value of the num variable which is 50 and the value after subtracting 10 utilizing the expr command which is 40 on the terminal.
Case 03: Subtract Integer From a Variable Using the “declare” Command
Here I have a variable named num. Here I will print the original value of the num variable. Then subtract 15 from the num variable utilizing the declare command and print the result on the terminal.
To create or update a variable using the declare command, follow the below script:
#!/bin/bash
#assigning and printing the value of num variable
num=70
echo "Original value: $num"
#converting num variable into integer using declare variable
declare -i num
#subtracting 15 from num variable
num=num-15
#printing the result
echo "Value after subtracting 15: $num"
The num=70
command has assigned a value to the num variable. Afterwards, the echo "Original value: $num"
command has printed the original value of the num variable on the terminal. Subsequently, the declare -i num
command has set the num
variable as an integer. Then the num=num-15
command subtracted the value of the num
variable by 15. Finally, the echo "Value after subtracting 15: $num"
command has printed the value after subtracting 15.
The image shows that the bash script has printed the original value of the num variable which is 70 and the value after subtracting 15 utilizing the declare command which is 55 on the terminal.
Example 04: Read Two Integer Numbers and Print the Subtraction of Both Variables
This example will develop a Bash script that will take two inputs from the user, subtract the second variable from the first variable, and print the result on the terminal.
To read two integer numbers and print their subtraction in Bash, you can use the following script:
#!/bin/bash
#taking input from the terminal
echo "Enter num1: "
read num1
echo "Enter num2: "
read num2
#subtracting num2 from num1
result=`expr $num1 - $num2`
# print the subtraction of both variables.
echo "Subtraction is: $result"
The echo "Enter num1: "
command prints a message asking for the value of the num1 variable as input from the user. Subsequently, the read num1 command has taken the input as the value of the num1 variable. Similarly, the echo "Enter num2: " read num2
command repeats the task for the num2
variable. After that, the result=`expr $num1 - $num2<strong>`</strong>
command is subtracted num2
from num1
. Finally, the echo "Subtraction is: $result"
command has printed the subtraction result on the terminal.
The bash script has taken two inputs num1 and num2 from the user with the help of the read command, subtracted num2 from num1 with the help of the expr command then printed the result on the terminal.
Conclusion
This is all about subtracting two bash variables. I believe after going through this article, you have become productive enough to explore subtracting bash variables, perform necessary mathematical and arithmetic operations for data processing, and make effective decisions from the result.
People Also Ask
How do you subtract two numbers in bash?
The easiest approach to subtracting two numbers in bash is the following approach.
result=`expr $num1 - $num2`
Here num1 and num2 are two variables. And the result holds the subtraction result.
How do you subtract two functions?
If you’re dealing with mathematical functions, you can find the difference between the outputs of two functions for specific inputs. In this case, you would evaluate each function separately and then subtract the results. Here’s an example in Bash:
#!/bin/bash
function f1 {
echo $((x + 5))
}
function f2 {
echo $((2 * x))
}
x=10
result=$(( $(f1) - $(f2) ))
echo "The result of subtracting f2 from f1 for x=$x is: $result"
In this example, f1
and f2
are two functions. The script then evaluates these functions for a specific value of x
and subtracts the result of f2
from the result of f1
.
How do you check if two variables are the same in bash?
Here you can use the == operator with the bash if statement to check if two strings are equal. You can also use != syntax to check if two strings are not equal. You must use a single space before and after the == and != syntax.
Which operator is used to subtract two numbers?
The operator used to subtract two numbers in Bash is the hyphen-minus (“-“) symbol. In an arithmetic context, you can use it to perform subtraction operations on numeric values. For example:
result=$((5 - 3))
echo $result
In this example, the expression 5 - 3
is evaluated, and the result is assigned to the variable result, which is then printed. The hyphen-minus serves as the subtraction operator in this arithmetic operation.
How to check variable value in Bash?
You can check the value of a variable in Bash by using the echo command or simply referencing the variable. Here’s an example:
my_variable="Hello, World!"
echo $my_variable
In this example, the value of the variable my_variable
is displayed using the echo Command. Alternatively, you can directly reference the variable without echo Command:
my_variable="Hello, World!"
echo "The value of my_variable is: $my_variable"
Both of these approaches will print the value of the variable to the terminal. If you want to check the variable’s value for debugging purposes, you can use echo or printf statements to display the value.
Related Articles
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial