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.
Key Takeaways
- Learning how to use the let command, expr command, and declare command to subtract bash variables.
- Learning about the process of taking two variables as input from the user and then subtracting one from another.
Free Downloads
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))
- Another straightforward approach is to subtract variables in bash using arithmetic expansion:
num=$((num-10))
- 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 follow the below syntax using the expr command:
result=`expr $num1 - $num2`
Here it updates the value in a new shell
- Using declare command. It converts the variable to an integer and then does the subtraction.
declare -i num num=num-15
4 Practical Examples Related to Subtracting Two Bash Variables
In this section, I will show you some approaches to subtracting two bash variables 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.
- At first, launch an Ubuntu Terminal.
- Write the following command to open a file in Nano:
nano script1.sh
EXPLANATION- nano: Opens the nano text editor.
- script1.sh: Bash script name.
- Copy 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"
EXPLANATION#! /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.
- Press CTRL+O and ENTER to save the file; CTRL+X to exit.
- Use the following command to make the file executable:
chmod +x script1.sh
EXPLANATION- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- script1.sh: File that you want to make executable.
- Run the script by the following command:
./script1.sh
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.
Script (script2.sh) >
#/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.
Run the script by executing the following command
./script2.sh
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. Here 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 know more, follow the below script.
Script (script3.sh) >
#/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. Afterward, 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.
Run the script by executing the following command.
./script3.sh
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 know more, follow the below script.
Script (script4.sh) >
#/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. Afterward, 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.
Run the script by executing the following command
./script4.sh
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 know more, follow the below script.
Script (script5.sh) >
#!/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. Afterward, 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.
Run the script by executing the following command
./script5.sh
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
In this example, I 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 know more, follow the below script.
Script (script6.sh) >
#!/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` command has subtracted num2 from num1. Finally, the echo “Subtraction is: $result” command has printed the subtraction result on the terminal.
Run the script by executing the following command.
./script6.sh
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
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