if / elif / else
The if statement evaluates the exit status of a command and branches execution accordingly. You can use [ ], [[ ]], or any regular command as the condition. Use elif for multiple branches and else for the fallback case.
Syntax
Basic syntax.
if command or condition; then
process
elif command or condition; then
process
else
process
fi
Using [ ] (equivalent to the test command).
if [ condition ]; then
Using [[ ]] (Bash extension — recommended).
if [[ condition ]]; then
Using logical operators.
if [ condition1 ] && [ condition2 ]; then if [[ condition1 && condition2 ]]; then
Common Conditions
| Condition | Description |
|---|---|
| [ -e file ] | True if the file or directory exists. |
| [ -f file ] | True if a regular file exists. |
| [ -d directory ] | True if the directory exists. |
| [ -z "$var" ] | True if the variable is an empty string. |
| [ -n "$var" ] | True if the variable is not empty. |
| [ "$a" = "$b" ] | True if the strings are equal (== also works). |
| [ "$a" != "$b" ] | True if the strings are not equal. |
| [ $a -eq $b ] | True if the integers are equal. |
| [ $a -ne $b ] | True if the integers are not equal. |
| [ $a -lt $b ] | True if a is less than b. |
| [ $a -gt $b ] | True if a is greater than b. |
| [[ $str =~ regex ]] | True if the string matches the regular expression ([[ ]] only). |
Sample Code
Use -f to check whether a file exists and print a message based on the result.
check_file.sh
#!/bin/bash
file="config.json"
if [ -f "$file" ]; then
echo "$file was found"
else
echo "$file was not found"
fi
bash check_file.sh
config.json was not found
You can also enter an if statement directly in the terminal. After pressing Enter following then, a > prompt appears — this signals that input is still expected. Typing fi executes the statement.
if [ -f "config.json" ]; then
echo "Found"
else
echo "Not found"
fi
Not found
Use multiple branches to compare numbers. -ge means "greater than or equal to".
grade.sh
score=85
if [ $score -ge 90 ]; then
echo "A (Excellent)"
elif [ $score -ge 80 ]; then
echo "B (Good)"
elif [ $score -ge 70 ]; then
echo "C (Pass)"
else
echo "D (Fail)"
fi
bash grade.sh
B (Good)
Use [[ ]] for string comparisons. It is also safe when you forget to quote variables.
check_env.sh
env="production"
if [[ "$env" = "production" ]]; then
echo "Running in production"
fi
bash check_env.sh
Running in production
Inside [[ ]], you can use && and || for logical operations.
check_age.sh
age=25
if [[ $age -ge 18 && $age -lt 65 ]]; then
echo "Working-age adult"
fi
bash check_age.sh
Working-age adult
Use the =~ operator inside [[ ]] to match against a regular expression.
check_email.sh
email="user@example.com"
if [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "Valid email address"
fi
bash check_email.sh
Valid email address
You can use a command's exit status directly as the condition. grep -q returns status 0 (true) on a match.
check_error.sh
# Check if app.log contains any errors
if grep -q "ERROR" app.log; then
echo "Errors were found"
fi
Notes
The difference between [ ] and [[ ]] matters. [ ] is equivalent to the POSIX test command — forgetting to quote variables can cause word splitting and unexpected errors. [[ ]] is a Bash extension that supports regex matching (=~), pattern matching, and the logical operators && ||, and it handles unquoted variables safely. For modern Bash scripts, [[ ]] is recommended.
For a full list of condition operators, see test / [ ] / [[ ]]. For multi-value branching, case is a convenient alternative.
If you find any errors or copyright issues, please contact us.