Bash Comparison Operators

Comparison operators are used to compare values and return true or false. They are essential for writing conditional logic in Bash scripts, especially when working with if, elif, while, and case statements.
This article explains how to compare integers, strings, and patterns in Bash.
Key Points
- Bash uses different operators for comparing integers and for comparing strings.
- Comparison operators can be used with single brackets
[ ](POSIX test), double brackets[[ ]](Bash-specific, safer and more powerful), and double parentheses(( ))(arithmetic context). - Bash variables are untyped. They are treated as integer or string depending on the context.
- Bash does not support floating-point operations. To compare floating point numbers, use an external tool such as
bcorawk.
Comparing Integers
To compare integers, you can use test operators with brackets or arithmetic operators with double parentheses.
Integer Comparison Operators
| Operator | Description |
|---|---|
-eq | Equal to |
-ne | Not equal to |
-gt | Greater than |
-ge | Greater than or equal to |
-lt | Less than |
-le | Less than or equal to |
Equal To Operator
The -eq operator checks if two integers are equal and returns true if they are:
#!/bin/bash
NUM1=5
NUM2=5
if [ "$NUM1" -eq "$NUM2" ]; then
echo "Numbers are equal."
else
echo "Numbers are not equal."
fiNumbers are equal.Not Equal To Operator
The -ne operator returns true if two integers are not equal:
#!/bin/bash
NUM1=5
NUM2=10
if [ "$NUM1" -ne "$NUM2" ]; then
echo "Numbers are not equal."
else
echo "Numbers are equal."
fiNumbers are not equal.Greater Than Operator
The -gt operator checks if the first integer is greater than the second and returns true if it is:
#!/bin/bash
NUM1=10
NUM2=5
if [ "$NUM1" -gt "$NUM2" ]; then
echo "$NUM1 is greater than $NUM2."
fi10 is greater than 5.Greater Than Or Equal Operator
The -ge operator returns true if the first integer is greater than or equal to the second:
#!/bin/bash
NUM1=10
NUM2=10
if [ "$NUM1" -ge "$NUM2" ]; then
echo "$NUM1 is greater than or equal to $NUM2."
fi10 is greater than or equal to 10.Less Than Operator
The -lt operator returns true if the first integer is less than the second:
#!/bin/bash
NUM1=5
NUM2=10
if [ "$NUM1" -lt "$NUM2" ]; then
echo "$NUM1 is less than $NUM2."
fi5 is less than 10.Less Than Or Equal Operator
The -le operator returns true if the first integer is less than or equal to the second:
#!/bin/bash
NUM1=5
NUM2=5
if [ "$NUM1" -le "$NUM2" ]; then
echo "$NUM1 is less than or equal to $NUM2."
fi5 is less than or equal to 5.Using Arithmetic Operators
You can also compare integers using arithmetic operators inside double parentheses (( )):
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
#!/bin/bash
NUM1=10
NUM2=5
if (( NUM1 > NUM2 )); then
echo "$NUM1 is greater than $NUM2."
fi10 is greater than 5.(( )), you do not need to add $ before variable names.Comparing Strings
String comparison operators check whether strings are equal or not equal, or compare them lexicographically (alphabetically).
String Comparison Operators
| Operator | Description |
|---|---|
= or == | Equal to |
!= | Not equal to |
< | Less than (lexicographically) |
> | Greater than (lexicographically) |
-z | String is empty |
-n | String is not empty |
=~ | Regex match |
Check if Two Strings are Equal
Use the = operator with single brackets or == with double brackets (recommended):
#!/bin/bash
VAR1="Linuxize"
VAR2="Linuxize"
if [ "$VAR1" = "$VAR2" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fiStrings are equal.Here’s how to use double brackets with user input:
#!/bin/bash
read -p "Enter first string: " VAR1
read -p "Enter second string: " VAR2
if [[ "$VAR1" == "$VAR2" ]]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fiEnter first string: Linuxize
Enter second string: Ubuntu
Strings are not equal.You can also use logical operators:
[[ "string1" == "string2" ]] && echo "Equal" || echo "Not equal"Not equalCheck if a String Contains a Substring
Use the * wildcard to match patterns in strings:
#!/bin/bash
VAR='GNU/Linux is an operating system'
if [[ $VAR == *"Linux"* ]]; then
echo "It's there."
fiIt's there.You can also use the =~ operator for regular expression matching:
#!/bin/bash
VAR='GNU/Linux is an operating system'
if [[ $VAR =~ .*Linux.* ]]; then
echo "It's there."
fiCheck if a String is Empty
Use -z to check if a string is empty, and -n to check if it is not empty:
#!/bin/bash
VAR=''
if [[ -z $VAR ]]; then
echo "String is empty."
fiString is empty.#!/bin/bash
VAR='Linuxize'
if [[ -n $VAR ]]; then
echo "String is not empty."
fiString is not empty.Comparing Strings with the Case Operator
You can also use the case statement to compare strings:
#!/bin/bash
VAR="Arch Linux"
case $VAR in
"Arch Linux")
echo "Arch matched"
;;
Fedora | CentOS)
echo "Red Hat based"
;;
esacArch matchedThe case statement is useful when you need to match a variable against several patterns.
Lexicographic Comparison
Lexicographic comparison compares strings alphabetically, character by character:
#!/bin/bash
VAR1="Linuxize"
VAR2="Ubuntu"
if [[ "$VAR1" > "$VAR2" ]]; then
echo "${VAR1} is lexicographically greater than ${VAR2}."
elif [[ "$VAR1" < "$VAR2" ]]; then
echo "${VAR2} is lexicographically greater than ${VAR1}."
else
echo "Strings are equal"
fiUbuntu is lexicographically greater than Linuxize.Comparing Floating-Point Numbers
Bash cannot compare floating-point numbers directly, but we can use utilities like bc or awk to make the comparison.
Using bc
NUM1=3.14
NUM2=2.71
if (( $(echo "$NUM1 > $NUM2" | bc -l) )); then
echo "$NUM1 is greater"
fiUsing awk
awk 'BEGIN { if (3.14 > 2.71) print "3.14 is greater" }'Important Notes
- Always use double quotes around variable names to avoid word splitting or globbing issues.
- Make sure to put a space between the operator and the operands.
- Use
=with single brackets[ ]and==with double brackets[[ ]]. - Inside double brackets,
<and>don’t need to be escaped. - Inside single brackets,
<and>must be escaped:\<and\>.
Quick Reference
For a printable quick reference, see the Bash cheatsheet .
| Operator | Context | Description |
|---|---|---|
-eq | [ ] [[ ]] | Integer equal to |
-ne | [ ] [[ ]] | Integer not equal to |
-gt | [ ] [[ ]] | Integer greater than |
-ge | [ ] [[ ]] | Integer greater than or equal to |
-lt | [ ] [[ ]] | Integer less than |
-le | [ ] [[ ]] | Integer less than or equal to |
== != > < | (( )) | Arithmetic comparison |
= or == | [ ] [[ ]] | String equal to |
!= | [ ] [[ ]] | String not equal to |
< > | [[ ]] | String lexicographic comparison |
-z | [ ] [[ ]] | String is empty |
-n | [ ] [[ ]] | String is not empty |
=~ | [[ ]] | String matches regex |
FAQ
What is the difference between [ ] and [[ ]] in Bash?[ ] is the POSIX test command and works in any POSIX-compatible shell. [[ ]] is a Bash-specific keyword that is safer and more powerful — it supports regex matching with =~, does not require quoting variables to prevent word splitting, and allows < and > without escaping. Prefer [[ ]] in Bash scripts.
Why does -eq not work for strings?-eq is an arithmetic operator and should be used only with integers. If you use it with non-numeric strings, Bash typically raises an integer-expression error instead of doing a meaningful comparison. Use = or == for string comparison.
Can I compare numbers with == inside single brackets?
No. Inside [ ], == is a string comparison, so [ 10 == 10 ] works but [ 10 == 9+1 ] does not — it compares the literal strings 10 and 9+1. Use -eq for integer comparison in [ ], or use (( 10 == 9+1 )) for arithmetic evaluation.
How do I check if a variable is greater than zero?
Use [[ $VAR -gt 0 ]] or (( VAR > 0 )). Both are equivalent in Bash.
Conclusion
Comparison operators in Bash allow you to compare integers and strings in your scripts. Use -eq, -ne, -gt, -ge, -lt, -le for integer comparison in brackets, or arithmetic operators like > and == inside (( )). For string comparison, prefer [[ ]] with ==, !=, -z, and -n.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page