๐Ÿ”„ 3. Bash Control Flow
Estimated reading: 3 minutes 46 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŸข Bash: Operators (-eq, -ne, -gt, etc.)

Or Copy Link

CONTENTS
Scroll to Top