🐚 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
ifconditions - File testing operators
- Real-world usage examples and best practices
Numeric Comparison Operators
These are used to compare integer values.
| Operator | Meaning | Example |
|---|---|---|
-eq | Equal to | [ "$a" -eq "$b" ] |
-ne | Not equal to | [ "$a" -ne "$b" ] |
-gt | Greater than | [ "$a" -gt "$b" ] |
-lt | Less than | [ "$a" -lt "$b" ] |
-ge | Greater or equal | [ "$a" -ge "$b" ] |
-le | Less 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.
| Operator | Meaning | Example |
|---|---|---|
= or == | Equal to | [ "$a" = "$b" ] |
!= | Not equal to | [ "$a" != "$b" ] |
-z | String is empty | [ -z "$a" ] |
-n | String 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.
| Operator | Checks for | Example |
|---|---|---|
-e | File or directory exists | [ -e file.txt ] |
-f | Regular file | [ -f script.sh ] |
-d | Directory | [ -d /etc ] |
-r | Readable | [ -r config.ini ] |
-w | Writable | [ -w log.txt ] |
-x | Executable | [ -x run.sh ] |
-s | File 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
| Mistake | Fix |
|---|---|
Using = instead of -eq for numbers | Use -eq, -gt, etc. for integers |
Comparing strings with [ $a = $b ] and unquoted vars | Quote 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,-nfor 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 :
