Bash operators explained banner
Bash focus guide

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.

On this page
Basics

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.

Spacing matters. Use [ -f "$FILE" ], not [-f "$FILE"].
Files

File test operators

OperatorMeaningExample
-fPath is a regular file[ -f "$FILE" ]
-dPath is a directory[ -d "$DIR" ]
-ePath exists[ -e "$PATH_TO_CHECK" ]
-xFile is executable[ -x "$SCRIPT" ]
-sFile exists and is not empty[ -s "$LOG" ]
-rFile is readable[ -r "$FILE" ]
-wFile is writable[ -w "$FILE" ]
FILE="/etc/passwd"

if [ -f "$FILE" ]; then
  echo "Regular file exists: $FILE"
fi
$ ./file-test.sh
Regular file exists: /etc/passwd
Strings

String operators

OperatorMeaningExample
-zString is empty[ -z "$NAME" ]
-nString 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
$ ./check-service.sh
Usage: ./check-service.sh service-name
Numbers

Numeric comparison operators

OperatorMeaningExample
-eqEqual[ "$A" -eq "$B" ]
-neNot equal[ "$A" -ne "$B" ]
-gtGreater than[ "$A" -gt "$B" ]
-geGreater than or equal[ "$A" -ge "$B" ]
-ltLess than[ "$A" -lt "$B" ]
-leLess than or equal[ "$A" -le "$B" ]
USE=94

if [ "$USE" -ge 90 ]; then
  echo "Disk critical: ${USE}%"
fi
$ ./disk-check.sh
Disk critical: 94%
Logic

Logic operators

You can combine conditions using logical operators.

OperatorMeaningExample
&&Run next command only if previous command succeedsmkdir backup && echo "created"
||Run next command only if previous command failssystemctl is-active --quiet nginx || echo "nginx down"
!Negates a conditionif ! systemctl is-active --quiet nginx; then
if [ -f "$FILE" ] && [ -s "$FILE" ]; then
  echo "File exists and is not empty"
fi
Arithmetic

Arithmetic operators

For arithmetic, Bash commonly uses (( ... )).

OperatorMeaningExample
+Add((total = a + b))
-Subtract((left = total - used))
*Multiply((size = count * 10))
/Divide((avg = total / count))
++Increment((count++))
count=0
((count++))
echo "$count"
$ ./count.sh
1
Practical scripts

Example 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
$ ./classify-path.sh /etc/passwd
Regular file: /etc/passwd

Example 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
$ ./disk-threshold.sh 84
[WARN] disk usage is 84%
Common errors

Common Bash operator mistakes

WrongWhy it breaksCorrect
[-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
./broken.sh: line 3: [-f: command not found
Quick reference

Bash operators quick reference

CategoryOperators
Files-f, -d, -e, -x, -s, -r, -w
Strings-z, -n, =, !=
Numbers-eq, -ne, -gt, -ge, -lt, -le
Logic&&, ||, !
Arithmetic+, -, *, /, ++, --
Related

Keep learning Bash

Decision helper

Which Bash operator should you use?

Need to checkUseExample
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 commandif systemctl is-active --quiet nginx; then ...
Output example

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
Example output:
$ ./check-file.sh
Usage: ./check-file.sh /path/to/file

$ ./check-file.sh /var/log/messages
OK: file exists

Related: Bash Arguments Explained, Bash User Input and Bash Scripting Hub.

FAQ

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.

$ practise_next --topic bash

Practise this next

Turn the guide into practice with a related quiz, builder, cheat sheet or learning path.