How to Subtract Two Bash Variables? [4+ Easy Approaches]

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:

  1. Straightforward approach to subtract variables in bash:
    ((num=num-10))
  2. Another straightforward approach is to subtract variables in bash using arithmetic expansion:
    num=$((num-10))
  3. Using let command:
    let "num=num-10"

    Here it updates the variable of the current shell.

  4. To subtract an integer from a variable follow the below syntax using the expr command:
    num=`expr $num - 10`
  5. 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

  6. 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.

Steps to Follow >

  1. At first, launch an Ubuntu Terminal.
  2. 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.
  3. 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.

  4. Press CTRL+O and ENTER to save the file; CTRL+X to exit.
  5. 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.
  6. Run the script by the following command:
    ./script1.sh

    The Bash script has two variables and subtracted the num2 variable from the num1 variable and then printed the result on the terminal.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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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"
EXPLANATION

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 bash script has two variables and subtracted the num2 variable from num1 variable utilizing the arithmetic expression and printed the result on the terminal.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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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"
EXPLANATION

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 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.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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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
EXPLANATION

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 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.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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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"
EXPLANATION

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 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.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.

You can follow the steps mentioned in Example 01 to know how to write, save, and make the bash script executable.

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"
EXPLANATION

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.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?

Most easiest approach to subtract 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 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 != 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 subtraction ( – ) operator subtracts the two operands, producing their difference.

How to check variable value in Bash?

A simple command, e.g., “echo Hello $Var_Name” will print “Hello [the value of the variable as defined]’. Bash will print nothing if the Var_Name variable is empty or not created.

Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

4.8/5 - (5 votes)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment