Bash Comparison Operators are fundamental elements in scripting that allow Bash programmers to compare values and make decisions based on results. These operators enable programmers to establish conditions, control the flow of the bash script, and create dynamic, responsive programs. Whether anyone compares numerical values, tests strings, or validates user input, Bash Comparison Operators provide the building blocks for intelligent decision-making within Bash Scripts. Here I will give you an overview of Bash comparison operators.
What are Comparison [Conditional] Operators in Bash?
Comparison operators also known as Conditional operators in Bash are used to evaluate conditions and make script decisions. These operators help determine whether a certain expression or comparison is true or false, allowing the script to take different paths based on the result.
Based on usage comparison operators are mainly of two types in Bash. These are:
- Bash comparison operators for comparing strings.
“$string1” != “$string2”
Here the!=
is the “is not equal to” comparison operator used for comparing strings.“$string1” > “$string2”
Here the>
is the “greater than” comparison operator used for comparing strings. - Bash comparison operators for comparing numbers.
"$number1" -gt "$number2"
Here-gt
is the “greater than” comparison operator used for comparing numbers.“$number1" -eq "$number2”
Here-eq
is the “is equal to” comparison operator used for comparing numbers.
Let’s obverse a sample code involving comparison operators:
if [[ $x -eq $y ]]
then
echo "x and y are equal!"
else
echo "x and y are not equal!"
fi
Here the -eq
is the “is equal to” comparison operator used for comparing the x and the y numbers.
There are also other comparison operators. In the subsequent section, I will discuss those operators.
List of Bash Operators for the Comparison of Strings [Lexicographic Comparison]
Operator | Meaning | Syntax |
---|---|---|
= |
|
|
== |
|
|
!= |
|
|
< |
|
|
> |
|
|
-z |
|
|
-n |
|
|
Example 1: Comparing Two Bash Strings Using Comparison Operator
The ==
comparison operator is used to compare two strings whether they are equal or not.
To compare two strings using the == comparison operator follow the syntax: "$string1" == "$string2"
. Here is an example.
#!/bin/bash
#defining string1 and string2
string1="Linux"
string2="Simply"
#comparing string1 and string2 and print a text according to comparison result
if [[ "$string1" == "$string2" ]]; then
echo "The strings are equal."
else
echo "The strings are different."
fi
Here, the "$string1" == "$string2"
command will compare if string1 is equal to string2. If the comparison result is true, it will print “The strings are equal”. If the comparison result is false, it will print “The strings are different” on the terminal.
The Bash script compared the two strings, string1, and string2, and found that the strings are different.
Read this article to learn more: How to Compare Strings in Bash With If Statement [6 Methods]
List of Bash Operators for the Comparison of Integer Numbers
Operator | Meaning | Syntax |
---|---|---|
-eq | is equal to | if [ “$a” -eq “$b” ] |
-ne | is not equal to | if [ “$a” -ne “$b” ] |
-gt | is greater than | if [ “$a” -gt “$b” ] |
-ge | is greater than or equal to | if [ “$a” -ge “$b” ] |
-lt | is less than | if [ “$a” -lt “$b” ] |
-le | is less than or equal to | if [ “$a” -le “$b” ] |
< | is less than | if ((“$a” < “$b”)) |
<= | is less than or equal to | if ((“$a” <= “$b”)) |
> | is greater than | if ((“$a” > “$b”)) |
>= | is greater than or equal to | if ((“$a” >= “$b”)) |
Example 2: Comparing Two Integer Numbers Using Comparison Operator
The -eq
comparison operator is a basic approach to comparing two integer numbers whether they are equal or not.
To compare two integer numbers using the -eq
comparison operator, you can use the syntax: "$number1" -eq "$number2"
. Here is how:
#!/bin/bash
# Script for equal to numeric comparison
number1=2
number2=2
if [[ $number1 -eq $number2 ]]
then
echo "number1 and number2 are equal!"
else
echo "number1 and number2 are not equal!"
fi
Here the $number1 -eq $number2
has compared whether variable number1 is equal to variable number2 and if the statement is true, “number1 and number2 are equal” text will be printed and if the comparison result is false then the text “number1 and number2 are not equal!” will be printed on the terminal.
The Bash script compared the two numbers number1, and number2, and found that the number1 and number2 are equal.
Read this article to learn more: How to Compare Numbers in Bash With If Statement [2 Methods]
Why Bash Comparison Operators are Necessary?
Bash comparison operators are necessary for:
- Enabling execution based on conditions.
- Making choices within scripts.
- Controlling loop execution based on conditions.
- Testing variable values for specific criteria.
- Validating command success to prevent issues.
- Ensuring user input meets specified criteria.
- Tasks like substring checks and string comparisons.
Conclusion
Bash comparison operators are one of the vital parts of Bash script. It facilitates the programmers to accomplish the comparison task and make the necessary decision from the result. In this article, I have tried to give you an overview of bash comparison operators and I believe after going through this article, you are competent enough to use this feature on your Bash scripting activities.
People Also Ask
What is greater than comparison in Bash?
Greater than comparison in Bash is used to test whether one value is greater than another. The -gt
is the operator for greater than comparison in Bash. It is an acronym for greater than. This -gt
operator allows programmers to compare two numeric values and returns a true (0) or false (1) result based on the comparison result.
What do comparison operators return?
Comparison operators return 0 or 1 according to the comparison result. You can compare numbers or strings with comparison operators. Expressions that use comparison operators do not return a number value as do arithmetic expressions. Comparison expressions return 1 the comparison result is true or 0, in case of false.
What is the function of the comparison operator?
The function of the comparison operator is to compare bash variables including numbers and strings and decide whether they are equal, not equal, which one is greater or which one is smaller, etc.
What are the operands of comparison operators?
The operands of comparison operators are the left and right components of the operators. You can compare the equality, inequality, or relative values of two operands of the same type based on using a relevant comparison operator. For example, “$string1” != “$string2”
compares if string1 and string2 are not equal.
Related Articles
- Arithmetic Operators in Bash
- Bash Logical Operators
- Unary Operators in Bash
- Usage of Ternary Operator in Bash [with 2 Examples]
- Bitwise Operators in Bash Scripting
<< Go Back to Bash Operator | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners