Compare Variables in Bash Scripts [3 Practical Cases]

The bash variable holds important data that carries vital information. Sometimes programmers might need to compare bash variables for processing and manipulation of data. In such cases, you need to know some techniques for comparing Bash variables. Some techniques are useful for both string and numeric variables, and some techniques are useful for only numeric variables. In this article, I have explored a bunch of techniques to compare bash variables. I have also included the syntax for these comparison techniques. So without further delay, let’s get started and deep dive into the realm of comparing bash variables.

Key Takeaways

  • Knowing syntax for checking equality, inequality, greater than, less than etc comparison.
  • Getting familiar with the process of equality, inequality, and greater than comparison.

Free Downloads

Operators to Compare Variable String

Here I have listed some common conditional expressions used for variable comparison with syntax and explanation.

Comparison Operator Syntax Explanation
is equal to (( $x == $y )) or -eq Returns true if both the integers in comparison are equal.
is not equal to (( $x != $y )) or -ne Returns true if both the integers in comparison are not equal.
is greater than (( $x > $y )) or -gt Returns true if x is greater than y.
is less than (( $x < $y )) Returns true if x is less than y.
is greater than or equal to (( $x >= $y )) Returns true if x is greater than or equals to y.
is less than or equal to (( $x <= $y )) Returns true if x is less than or equals to y.

3 Cases to Compare Variables in Bash Script

Variables store data and sometimes you might need to compare Bash variables to process and manipulate their data. In this section, I have listed some cases of comparing variables. I hope after going through these cases, you will be able to compare variables productively.

Case 01: Compare String Variables Using the ‘==’ Operator or “-eq” Command

You can easily string variables using the “==” operator. If the variable is numerical, you can use the “eq” command in place of the “==” operator. And syntax for using the -eq command is if [[ $variable_1 -eq $variable_2 ]], Here, I will compare two string variables with the “==” operator. To do so, follow the below procedures.

Steps to Follow >

❶ 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.
  • sh: Bash script name.

➌ Copy the script mentioned below:

#!bin/bash
#setting value of name1 and name2 variable
name1="Mark"
name2="Jhon"
#checking the equality of the name1 and name2 variable.
if [ "$name1" == "$name2" ]; then
echo "The names are the same."
else
echo "The names are different."
fi
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, name1=”Mark” name2=”Jhon” command has assigned value to the name1 and name2 variable. Afterward, if [ “$name1” == “$name2” ]; the command compares the name1 and name2 variables and prints the “The names are the same” message if the comparison result is true otherwise prints the “The names are different.” message on the terminal. Finally, fi denotes the end of the if command.

❹ 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.
  • sh: File that you want to make executable.

❻ Run the script by the following command:

./script1.sh

The Bash script has compared two string variables whether they are equal and printed a message accordingly.The image shows that the Bash script has compared two string variables and printed a message accordingly.

Case 02: Compare Variables Using the ‘!=’ Operator or the “-ne” Command

The programmers can easily compare two variables using the != operator or -ne command. The syntax for using the -ne command is if [[ $variable_1 -ne $variable_2 ]]. If the variable is a string, the only way is to use the != operator. Here I will develop a Bash script that will utilize the != operator to compare if two numeric variable is not equal or not. To know more, follow the below script.

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

Script (script2.sh) >

#!/bin/bash
#assigning value to code1 and code2 variable
code1="7845"
code2="9632"
#checking the inequality of the code1 and code2 variable
if [ "$code1" != "$code2" ]; then
#output of the if condition will be true if the two variable is unequal
echo "The codes are different."
else
echo "The codes are the same."
fi
EXPLANATION

The code1=”7845″ code2=”9632″ command has assigned value to the code1 and code2 variables. Then the if [ “$code1” != “$code2” ]; command has checked the inequality of values of the code1 and code2 variables and returns exit status as true if the values are unequal otherwise false. If the exit status is true, it prints the “The codes are different.” message on the terminal otherwise it prints the “The codes are the same.” message on the terminal. Finally, the fi command denotes the end of the if condition.

Run the script by executing the following command

./script2.sh

The Bash script has compared two numeric variables whether they are not equal and printed a message accordingly.The image shows that the Bash script has compared two numeric variables whether they are not equal or equal and printed a message accordingly.

Case 03: Check if a Variable is Greater than Another Variable

You can check whether a variable is greater than other variables simply just using the “>=” operator or “-gt” command. If the variables are string then using >= operator is the only option. Here I have developed a Bash script to check whether a numeric variable is greater than another numeric variable using the -gt command. To know more, follow the below script.

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

Script (script3.sh) >

!/bin/bash
#assigning values to the x and y variables
x=3
y=1
#checking whether x is greater than y. If true, the comparison will return zero as the exit status
if [[ $x -gt $y ]]
then
echo "x is greater than y!"
else
echo "x less than or equal to y!"
fi
EXPLANATION

The x=3 y=1 command has assigned values to the x and y variables. After that the if [[ $x -gt $y ]] command has started an if condition where it checks whether x is greater than y. If yes then it returns a zero exit status. And in such conditions, it prints the “x is greater than y!” message on the terminal. Otherwise, it prints the “x less than or equal to y!” message on the terminal. Finally, fi denotes the end of the if condition.

Run the script by executing the following command

./script3.sh

The Bash script has checked whether variable x is greater than y or not. And print a message on the terminal accordingly.The image shows that the Bash script has checked whether variable x is greater than y or not. And print a message on the terminal accordingly.

Conclusion

Comparing Bash variables is an important task for the programmer as it enables the programmer to compare the valuable data stored inside a variable. In this article, I have tried to explore the various techniques for comparing bash variables and I believe these will be helpful for you to learn the process of comparing bash variables and use it effectively.

People Also Ask

How do you compare two variables in Bash?
You can compare string variables in Bash using various operators such as ‘==‘ and ‘!=‘. These operators are used in an ‘if’ statement to check for certain conditions.
Are Bash variables case-sensitive?
Bash variables are case-sensitive. The case of the character matters. For this reason, variable_1 and Variable_2 are two separate variables.
How to store data in a variable in Bash?
You need to add a $ symbol before the variable name to store data in a variable in Bash. It will assign a new value to the variable or create a variable if it does not exist.
For what purpose are variables used?
The variable is a placeholder for an unknown quantity. In math, variables are used to find general equations and then later substitute specific values for the variable.

Related Articles


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

5/5 - (6 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