🔄 3. Bash Control Flow
Estimated reading: 3 minutes 358 views

🐚 Bash Operators – -eq, -ne, -gt, and Other Comparison Operators Explained


Introduction to Bash Operators – Use -eq, -ne, -gt and More in Conditions

Bash provides a rich set of comparison operators to perform logic in conditional statements. These operators are used with if, while, and case to compare numbers, strings, and file attributes.

The most commonly used numeric comparison operators include -eq, -ne, -gt, -lt, -ge, and -le. Knowing how and when to use these helps you build intelligent Bash scripts that respond dynamically to system states or user inputs.


In this article, you’ll learn:

  • Numeric and string comparison operators in Bash
  • How to use operators in if conditions
  • File testing operators
  • Real-world usage examples and best practices

Numeric Comparison Operators

These are used to compare integer values.

OperatorMeaningExample
-eqEqual to[ "$a" -eq "$b" ]
-neNot equal to[ "$a" -ne "$b" ]
-gtGreater than[ "$a" -gt "$b" ]
-ltLess than[ "$a" -lt "$b" ]
-geGreater or equal[ "$a" -ge "$b" ]
-leLess or equal[ "$a" -le "$b" ]

Example: Number Comparison

a=10
b=20

if [ "$a" -lt "$b" ]; then
  echo "$a is less than $b"
fi

Output:

10 is less than 20

String Comparison Operators

Used for comparing textual values.

OperatorMeaningExample
= or ==Equal to[ "$a" = "$b" ]
!=Not equal to[ "$a" != "$b" ]
-zString is empty[ -z "$a" ]
-nString is not empty[ -n "$a" ]

Use double square brackets [[ ... ]] when working with strings to enable pattern matching and safer evaluations.


Example: String Check

name="bash"

if [[ $name == "bash" ]]; then
  echo "Match found"
fi

Output:

Match found

File Test Operators

These are used to test files and directories.

OperatorChecks forExample
-eFile or directory exists[ -e file.txt ]
-fRegular file[ -f script.sh ]
-dDirectory[ -d /etc ]
-rReadable[ -r config.ini ]
-wWritable[ -w log.txt ]
-xExecutable[ -x run.sh ]
-sFile is not empty[ -s data.csv ]

Example: File Exists

if [ -e "myfile.txt" ]; then
  echo "File exists."
else
  echo "File not found."
fi

Arithmetic Evaluation with (( ))

For math-style comparisons:

a=5
b=10

if (( a < b )); then
  echo "$a is less than $b"
fi

Output:

5 is less than 10

(( )) is more readable for numeric logic and allows arithmetic operations directly.


Common Pitfalls to Avoid

MistakeFix
Using = instead of -eq for numbersUse -eq, -gt, etc. for integers
Comparing strings with [ $a = $b ] and unquoted varsQuote your variables to avoid errors
Using == inside [ ]Only use == in [[ ]] or with bash

Summary – Bash Operators

Bash operators give your scripts the power to evaluate conditions, make decisions, and control flow dynamically. Whether comparing numbers, strings, or files, choosing the right operator ensures your script behaves predictably.

Key Takeaways:

  • Use -eq, -gt, etc. for numeric comparisons
  • Use =, !=, -z, -n for strings
  • Use -e, -f, -d, etc. for file tests
  • Prefer [[ ]] or (( )) for safer expressions

Real-world Uses:

  • Validate user input
  • Compare configuration states
  • Check file presence before processing

FAQ – Bash Operators


What is the difference between -eq and == in Bash?
-eq is for numeric comparison, while == is used for string comparison inside [[ ]].


How do I check if a file exists in Bash?
Use:

if [ -e filename ]; then
  echo "Exists"
fi

Can I use < and > in Bash conditions?
Only inside (( )) for numeric values:

if (( a < b )); then echo "yes"; fi

How do I test if a string is empty?
Use:

if [ -z "$str" ]; then echo "Empty"; fi

Should I quote variables in conditions?
Yes! Always quote variables to prevent errors due to spaces or null values:

if [ "$a" = "$b" ]; then ...

Share Now :
Share

🟢 Bash: Operators (-eq, -ne, -gt, etc.)

Or Copy Link

CONTENTS
Scroll to Top