In shell or any other programming language, equality operators are widely used to assign or compare two values or variables. Without these operators, programming can be difficult, and it could take much more time for a simple task. Shell supports three types of equality operators such as “=, ==, -eq.”
This guide explores the shell equality operators =, ==, -eq and their usage in the bash script.
- The = Operator
- The == Operator
- The -eq Operator
- Difference Between Equality Operators (=, ==, -eq)
What is the = Operator in Shell and How to Use it?
The “=” operator in the shell assigns the values to the variable and can also check the equality of two strings. For example, if we are to check if the string1 is equal to the string2, we’d use it in the bash script like this:
#!/bin/bash
string1="itslinux"
string2="foss"
if [ "$string1" = "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
In the above-written script, if the values of string1 and string2 are matched, it displays “The strings are equal.” If they are not, it displays “The strings are not equal.”
To execute the above-created script (name script.sh), use the “bash” command as below:
$ bash script.sh
After executing the above script, “The strings are not equal” is displayed because the strings are not matched in both variables.
What is the == Operator in Shell and How to Use it?
The “==” equality operator is the same as the “=” equality operator, but since the “=” is also used to assign values, it could sometimes confuse users. This is why most of the programmers/developers use the double equal (==) for comparison:
#!/bin/bash
string1="itslinux"
string2="foss"
if [ "$string1" == "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
In this script if the values of string1 and string2 are matched, it displays “The strings are equal.” If they are not, it displays “The strings are not equal.”
By executing the above script, the following output is displayed:
$ bash script.sh
The “==” is the same as “=” in terms of comparison but is considered a modern approach.
What is the -eq Operator in Shell and How to Use it?
The “==” and “=” can only be used to compare the strings, but what if you want to compare the integers (numerical values)? For this, we have the “-eq” operator, and here is how it is used:
#!/bin/bash
num1=10
num2=10
if [ $num1 -eq $num2 ]; then
echo "The values are equal."
else
echo "The values are not equal."
fi
In the above script, the values of “num1 and num2” are compared and if they are the same, “The values are equal” is printed otherwise, “The values are not equal” is printed.
To execute the above script, use this command:
$ bash script.sh
The above execution displays “The values are equal” which shows both the variables were assigned the same values.
Difference Between Equality Operators (=, ==, -eq) in Shell
Let’s discuss the difference between the equality operators in the shell:
- The “=” operator is used to test whether two strings are equal, and it can also be used to assign values to the variables.
- The “==” operator is also used to test whether two strings are equal, similar to the “=” operator
- The “-eq” operator can only be utilized to test whether two integers are equal.
Conclusion
The shell equality operators (=, ==, -eq) are mainly used for the comparison of the values stored in the variables. The “= and ==” is for string comparison, while “-eq” is used to compare numerical values. The single equality operator (‘=’) is required to assign a value to a variable.
This guide explained the shell equality operators (=, ==, -eq) and how to use them in Linux.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY