Conditionals

Bash conditionals look a little alien at first — the spaces inside [ ] aren't optional, and there are separate operators for comparing numbers versus comparing text.

if / elif / else

bash check-age.sh
#!/bin/bash
age=20

if [[ $age -lt 13 ]]; then
    echo "Child"
elif [[ $age -lt 20 ]]; then
    echo "Teenager"
else
    echo "Adult"
fi
Terminal output
Adult

Bash blocks end with a keyword spelled backwards — if ends with fi. It reads oddly at first but becomes second nature fast.

Numbers vs. strings

Numeric comparisons use -eq, -ne, -lt, -gt, -le, -ge. String comparisons use == and !=. Mixing them up either fails or silently does the wrong thing:

bash compare.sh
#!/bin/bash
name="admin"
count=5

if [[ "$name" == "admin" ]]; then
    echo "Welcome, admin"
fi

if [[ $count -ge 5 ]]; then
    echo "Count reached the limit"
fi
Terminal output
Welcome, admin
Count reached the limit

Checking files

File test operators check things about the filesystem directly inside a condition — -f for "is a regular file," -d for "is a directory," -e for "exists at all":

bash check-file.sh
#!/bin/bash
if [[ -f "config.txt" ]]; then
    echo "Found config.txt"
else
    echo "config.txt is missing"
fi
Terminal output
config.txt is missing
[ vs [[ : the older [ ... ] (a real command, an alias for test) requires careful quoting — [ $name == admin ] breaks entirely if $name is empty or contains spaces, because it can turn into [ == admin ], which is invalid. The newer [[ ... ]] is a shell keyword that handles unquoted variables and empty values safely. Prefer [[ ]] in Bash scripts unless you specifically need POSIX sh compatibility.