๐ 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 :
