Comparison Operators in Bash
Bash comparison operators are used to compare numbers or strings in shell scripts. In this tutorial, we shall go through Bash comparison operators, including their syntax and examples for each of them.
Types of Comparison Operators
Operator | Description |
---|---|
-eq | Checks if two numbers are equal |
-ne | Checks if two numbers are not equal |
-gt | Checks if the first number is greater than the second |
-lt | Checks if the first number is less than the second |
-ge | Checks if the first number is greater than or equal to the second |
-le | Checks if the first number is less than or equal to the second |
= | Checks if two strings are equal |
!= | Checks if two strings are not equal |
-z | Checks if a string is empty |
-n | Checks if a string is not empty |
1. -eq
: Equal
The -eq
operator checks if two numbers are equal. If the numbers are equal, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ num1 -eq num2 ]
Example
example_eq.sh
#!/bin/bash
num1=10
num2=10
if [ $num1 -eq $num2 ]; then
echo "The numbers are equal."
else
echo "The numbers are not equal."
fi
Output
The numbers are equal.
2. -ne
: Not Equal
The -ne
operator checks if two numbers are not equal. If the numbers are not equal, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ num1 -ne num2 ]
Example
example_ne.sh
#!/bin/bash
num1=10
num2=20
if [ $num1 -ne $num2 ]; then
echo "The numbers are not equal."
else
echo "The numbers are equal."
fi
Output
The numbers are not equal.
3. -gt
: Greater Than
The -gt
operator checks if the first number is greater than the second number. If true, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ num1 -gt num2 ]
Example
example_gt.sh
#!/bin/bash
num1=15
num2=10
if [ $num1 -gt $num2 ]; then
echo "$num1 is greater than $num2."
else
echo "$num1 is not greater than $num2."
fi
Output
15 is greater than 10.
4. -lt
: Less Than
The -lt
operator checks if the first number is less than the second number. If true, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ num1 -lt num2 ]
Example
example_lt.sh
#!/bin/bash
num1=5
num2=10
if [ $num1 -lt $num2 ]; then
echo "$num1 is less than $num2."
else
echo "$num1 is not less than $num2."
fi
Output
5 is less than 10.
5. -ge
: Greater Than or Equal To
The -ge
operator checks if the first number is greater than or equal to the second number. If true, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ num1 -ge num2 ]
Example
example_ge.sh
#!/bin/bash
num1=20
num2=20
if [ $num1 -ge $num2 ]; then
echo "$num1 is greater than or equal to $num2."
else
echo "$num1 is less than $num2."
fi
Output
20 is greater than or equal to 20.
6. -le
: Less Than or Equal To
The -le
operator checks if the first number is less than or equal to the second number. If true, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ num1 -le num2 ]
Example
example_le.sh
#!/bin/bash
num1=15
num2=20
if [ $num1 -le $num2 ]; then
echo "$num1 is less than or equal to $num2."
else
echo "$num1 is greater than $num2."
fi
Output
15 is less than or equal to 20.
7. =
: String Equality
The =
operator checks if two strings are equal. If the strings match, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ str1 = str2 ]
Example
example_eq_string.sh
#!/bin/bash
str1="hello"
str2="hello"
if [ $str1 = $str2 ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
Output
The strings are equal.
8. -z
: String is Empty
The -z
operator checks if a string is empty. If the string is empty, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ -z str ]
Example
example_z.sh
#!/bin/bash
str=""
if [ -z $str ]; then
echo "The string is empty."
else
echo "The string is not empty."
fi
Output
The string is empty.
9. !=
: String Not Equal
The !=
operator checks if two strings are not equal. If the strings are different, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ str1 != str2 ]
Example
example_ne_string.sh
#!/bin/bash
str1="hello"
str2="world"
if [ $str1 != $str2 ]; then
echo "The strings are not equal."
else
echo "The strings are equal."
fi
Output
The strings are not equal.
10. -n
: String Is Not Empty
The -n
operator checks if a string is not empty. If the string contains at least one character, the condition evaluates to true; otherwise, it evaluates to false.
Syntax
[ -n str ]
Example
example_n.sh
#!/bin/bash
str="hello"
if [ -n $str ]; then
echo "The string is not empty."
else
echo "The string is empty."
fi
Output
The string is not empty.