
Bash Operators Explained
Bash operators help scripts make decisions. This guide explains file tests, string checks, numeric comparisons, logic operators and practical if statement patterns for Linux beginners.
What are Bash operators?
Operators are symbols or flags that compare values or test conditions. They are usually used inside if statements.
if [ -f "$FILE" ]; then
echo "File exists"
fi
In that example, -f is a file test operator. It checks whether the path is a regular file.
[ -f "$FILE" ], not [-f "$FILE"].File test operators
| Operator | Meaning | Example |
|---|---|---|
-f | Path is a regular file | [ -f "$FILE" ] |
-d | Path is a directory | [ -d "$DIR" ] |
-e | Path exists | [ -e "$PATH_TO_CHECK" ] |
-x | File is executable | [ -x "$SCRIPT" ] |
-s | File exists and is not empty | [ -s "$LOG" ] |
-r | File is readable | [ -r "$FILE" ] |
-w | File is writable | [ -w "$FILE" ] |
FILE="/etc/passwd"
if [ -f "$FILE" ]; then
echo "Regular file exists: $FILE"
fi
Regular file exists: /etc/passwdString operators
| Operator | Meaning | Example |
|---|---|---|
-z | String is empty | [ -z "$NAME" ] |
-n | String is not empty | [ -n "$NAME" ] |
= | Strings are equal | [ "$A" = "$B" ] |
!= | Strings are not equal | [ "$A" != "$B" ] |
SERVICE="$1"
if [ -z "$SERVICE" ]; then
echo "Usage: $0 service-name"
exit 1
fi
Usage: ./check-service.sh service-nameNumeric comparison operators
| Operator | Meaning | Example |
|---|---|---|
-eq | Equal | [ "$A" -eq "$B" ] |
-ne | Not equal | [ "$A" -ne "$B" ] |
-gt | Greater than | [ "$A" -gt "$B" ] |
-ge | Greater than or equal | [ "$A" -ge "$B" ] |
-lt | Less than | [ "$A" -lt "$B" ] |
-le | Less than or equal | [ "$A" -le "$B" ] |
USE=94
if [ "$USE" -ge 90 ]; then
echo "Disk critical: ${USE}%"
fi
Disk critical: 94%Logic operators
You can combine conditions using logical operators.
| Operator | Meaning | Example |
|---|---|---|
&& | Run next command only if previous command succeeds | mkdir backup && echo "created" |
|| | Run next command only if previous command fails | systemctl is-active --quiet nginx || echo "nginx down" |
! | Negates a condition | if ! systemctl is-active --quiet nginx; then |
if [ -f "$FILE" ] && [ -s "$FILE" ]; then
echo "File exists and is not empty"
fi
Arithmetic operators
For arithmetic, Bash commonly uses (( ... )).
| Operator | Meaning | Example |
|---|---|---|
+ | Add | ((total = a + b)) |
- | Subtract | ((left = total - used)) |
* | Multiply | ((size = count * 10)) |
/ | Divide | ((avg = total / count)) |
++ | Increment | ((count++)) |
count=0
((count++))
echo "$count"
1Example script: classify a path
#!/usr/bin/env bash
TARGET="$1"
if [ -z "$TARGET" ]; then
echo "Usage: $0 path"
exit 1
fi
if [ -f "$TARGET" ]; then
echo "Regular file: $TARGET"
elif [ -d "$TARGET" ]; then
echo "Directory: $TARGET"
elif [ -e "$TARGET" ]; then
echo "Exists, but is not a regular file or directory: $TARGET"
else
echo "Missing: $TARGET"
exit 1
fi
Regular file: /etc/passwdExample script: disk threshold check
#!/usr/bin/env bash
USE="$1"
if [ -z "$USE" ]; then
echo "Usage: $0 percentage"
exit 1
fi
if [ "$USE" -ge 90 ]; then
echo "[CRITICAL] disk usage is ${USE}%"
elif [ "$USE" -ge 80 ]; then
echo "[WARN] disk usage is ${USE}%"
else
echo "[OK] disk usage is ${USE}%"
fi
[WARN] disk usage is 84%Common Bash operator mistakes
| Wrong | Why it breaks | Correct |
|---|---|---|
[-f "$FILE"] | Missing spaces around brackets. | [ -f "$FILE" ] |
[ $FILE = test ] | Unquoted variable can break if empty or contains spaces. | [ "$FILE" = "test" ] |
[ "$USE" > 90 ] | String comparison, not numeric comparison. | [ "$USE" -gt 90 ] |
[ "$NAME" -eq "bob" ] | -eq is for numbers, not strings. | [ "$NAME" = "bob" ] |
./broken.sh: line 3: [-f: command not foundBash operators quick reference
| Category | Operators |
|---|---|
| Files | -f, -d, -e, -x, -s, -r, -w |
| Strings | -z, -n, =, != |
| Numbers | -eq, -ne, -gt, -ge, -lt, -le |
| Logic | &&, ||, ! |
| Arithmetic | +, -, *, /, ++, -- |
Keep learning Bash
Which Bash operator should you use?
| Need to check | Use | Example |
|---|---|---|
| File exists and is a regular file | -f | [ -f "$file" ] |
| Directory exists | -d | [ -d "$path" ] |
| String is empty | -z | [ -z "$name" ] |
| String is not empty | -n | [ -n "$name" ] |
| Numbers are equal | -eq | [ "$count" -eq 10 ] |
| Command succeeded | $? or direct if command | if systemctl is-active --quiet nginx; then ... |
Example: combine file and string checks
#!/usr/bin/env bash
file="$1"
if [ -z "$file" ]; then
echo "Usage: $0 /path/to/file"
exit 1
fi
if [ -f "$file" ]; then
echo "OK: file exists"
else
echo "ERROR: not a regular file"
exit 2
fi
$ ./check-file.sh
Usage: ./check-file.sh /path/to/file
$ ./check-file.sh /var/log/messages
OK: file existsRelated: Bash Arguments Explained, Bash User Input and Bash Scripting Hub.
Frequently Asked Questions
What is the difference between -eq and = in Bash?
-eq compares numbers, while = compares strings inside test brackets.
Should I quote variables in Bash tests?
Yes. Quoting variables helps avoid errors when values are empty or contain spaces.
What does -z mean in Bash?
-z checks whether a string has zero length, meaning it is empty.
Can I use command results directly in if statements?
Yes. A common pattern is if command; then ... because Bash uses the command exit status.