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

OperatorDescription
-eqChecks if two numbers are equal
-neChecks if two numbers are not equal
-gtChecks if the first number is greater than the second
-ltChecks if the first number is less than the second
-geChecks if the first number is greater than or equal to the second
-leChecks 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
-zChecks if a string is empty
-nChecks if a string is not empty
Comparison Operators in Bash

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

</>
Copy
[ num1 -eq num2 ]

Example

example_eq.sh

</>
Copy
#!/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

</>
Copy
[ num1 -ne num2 ]

Example

example_ne.sh

</>
Copy
#!/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

</>
Copy
[ num1 -gt num2 ]

Example

example_gt.sh

</>
Copy
#!/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

</>
Copy
[ num1 -lt num2 ]

Example

example_lt.sh

</>
Copy
#!/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

</>
Copy
[ num1 -ge num2 ]

Example

example_ge.sh

</>
Copy
#!/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

</>
Copy
[ num1 -le num2 ]

Example

example_le.sh

</>
Copy
#!/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

</>
Copy
[ str1 = str2 ]

Example

example_eq_string.sh

</>
Copy
#!/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

</>
Copy
[ -z str ]

Example

example_z.sh

</>
Copy
#!/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

</>
Copy
[ str1 != str2 ]

Example

example_ne_string.sh

</>
Copy
#!/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

</>
Copy
[ -n str ]

Example

example_n.sh

</>
Copy
#!/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.