Conditionals
Scripts need to make decisions. "If this, then that" - the foundation of programming.
Basic if Statement
#!/bin/bash
if [ -f "config.txt" ]; then
echo "Config file exists"
fi
Spaces Are Critical
[ -f "file" ] โ (spaces after [ and before ])
[-f "file"] โ (will error)
if-else
if [ -f "config.txt" ]; then
echo "Loading config..."
else
echo "Config not found, using defaults"
fi
if-elif-else
#!/bin/bash
score=75
if [ "$score" -ge 90 ]; then
echo "Grade: A"
elif [ "$score" -ge 80 ]; then
echo "Grade: B"
elif [ "$score" -ge 70 ]; then
echo "Grade: C"
else
echo "Grade: F"
fi
Single Bracket vs Double Bracket
Single Bracket [ ] (POSIX)
if [ "$name" = "John" ]; then # string comparison
if [ "$age" -eq 30 ]; then # number comparison
Double Bracket [[ ]] (Bash)
if [[ "$name" == "John" ]]; then # string comparison
if [[ "$age" -eq 30 ]]; then # number comparison
if [[ "$file" == *.txt ]]; then # pattern matching!
Prefer [[ ]]
Double brackets are safer (no word splitting issues) and support more features. Use them unless you need POSIX compatibility.
File Tests
| Test | True If |
|---|---|
-e file | File exists |
-f file | Is regular file |
-d file | Is directory |
-r file | Is readable |
-w file | Is writable |
-x file | Is executable |
-s file | File size > 0 |
#!/bin/bash
if [[ -d "/var/log" ]]; then
echo "/var/log is a directory"
fi
if [[ -x "./script.sh" ]]; then
./script.sh
else
echo "Script not executable"
fi
String Tests
| Test | True If |
|---|---|
-z "$str" | String is empty |
-n "$str" | String is not empty |
"$a" == "$b" | Strings are equal |
"$a" != "$b" | Strings differ |
#!/bin/bash
name=""
if [[ -z "$name" ]]; then
echo "Name is empty"
fi
if [[ "$USER" == "root" ]]; then
echo "Running as root"
fi
Combining Conditions
AND (&&)
if [[ -f "file.txt" && -r "file.txt" ]]; then
echo "File exists and is readable"
fi
OR (||)
if [[ "$1" == "start" || "$1" == "run" ]]; then
echo "Starting..."
fi
NOT (!)
if [[ ! -f "lock.file" ]]; then
echo "No lock file, safe to proceed"
fi
Short-Circuit Evaluation
# Run command only if file exists
[[ -f "script.sh" ]] && ./script.sh
# Run command only if file doesn't exist
[[ -f "config.txt" ]] || echo "Config missing!"
# Common pattern
[[ -d "$dir" ]] || mkdir -p "$dir"
The test Command
[ ] is actually the test command:
Practical Example
#!/bin/bash
# Backup script with checks
backup_dir="/backup"
source_dir="/var/www"
# Check if running as root
if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root"
exit 1
fi
# Create backup dir if missing
if [[ ! -d "$backup_dir" ]]; then
mkdir -p "$backup_dir"
fi
# Check source exists
if [[ -d "$source_dir" ]]; then
cp -r "$source_dir" "$backup_dir"
echo "Backup complete"
else
echo "Source directory not found"
exit 1
fi
What does the test [ -z $var ] check?
Quick Reference
| Syntax | Purpose |
|---|---|
if [[ ]]; then ... fi | Basic if |
if ... else ... fi | If-else |
if ... elif ... else ... fi | Multiple conditions |
-f file | File exists |
-d dir | Directory exists |
-z "$var" | String is empty |
$a == $b | Strings equal |
$a -eq $b | Numbers equal |
&& / ` |
Key Takeaways
if [[ condition ]]; then ... fiis the basic structure- Spaces inside brackets are required
- Use
[[ ]]for bash scripts (safer, more features) -ftests files,-dtests directories-zchecks empty strings,-nchecks non-empty- Combine with
&&(and),||(or),!(not)
Next: numeric comparison operators in detail.