An Overview of Bash Comparison [Conditional] Operators

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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:

  1. 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.
  2.  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
=
  • is equal to
  • if [ “$a” = “$b” ]
==
  • is equal to
  • The == comparison operator behaves differently within a double-brackets test than within single brackets.
  • [[ $a == z* ]] True if $a starts with a “z” (pattern matching).
  • [[ $a == “z*” ]] True if $a is equal to z* (literal matching).
  • [ $a == z* ] File globbing and word splitting take place.
  • [ “$a” == “z*” ] True if $a is equal to z* (literal matching).
!=
  • is not equal to
  • if [ “$a” != “$b” ]
  • This operator uses pattern matching within a [[ … ]] construct.
<
  • is less than, in ASCII alphabetical order.
  • The < needs to be escaped within a [ ] construct.
  • if [[ “$a” < “$b” ]]
  • if [ “$a” \< “$b” ]
>
  • is greater than, in ASCII alphabetical order.
  • The > needs to be escaped within a [ ] construct.
  • if [[ “$a” > “$b” ]]
  • if [ “$a” \> “$b” ]
-z
  • string is null
  • It has zero length
  • if [ -z “$s” ]
-n
  • the string is not null.
  • if [ -n “$s” ]

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
EXPLANATION

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 has checked the equality of string1 and string2 and found that they are not equal.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
EXPLANATION

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 has checked the equality of number1 and number2 and found that they are equal.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:

  1. Enabling execution based on conditions.
  2. Making choices within scripts.
  3. Controlling loop execution based on conditions.
  4. Testing variable values for specific criteria.
  5. Validating command success to prevent issues.
  6. Ensuring user input meets specified criteria.
  7. 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


<< Go Back to Bash Operator | Bash Scripting Tutorial

Rate this post
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