Bash 5 reference · Shell Builtins and Syntax
if (Bash builtin)
Run commands depending on whether a command succeeds (exit status 0), with optional elif and else branches.
Bash 5
Syntax
if COMMANDS; then ...; elif COMMANDS; then ...; else ...; fi
Parameters
| Parameter | Description |
|---|---|
condition |
Any command; [[ ]], (( )) and grep -q are common. |
Examples
for n in 3 12; do
if (( n > 10 )); then echo "$n is big"; elif (( n > 5 )); then echo "$n is medium"; else echo "$n is small"; fi
done
Output
3 is small 12 is big
Run this on your computer — see “Get Started” for setup.
echo 'ERROR disk full' > app.log
if grep -q ERROR app.log; then echo 'errors found'; fi
Output
errors found
Run this on your computer — see “Get Started” for setup.
Related
[[ ]]· Bash's conditional expression: compare strings and numbers, test files, and match glob patterns or regular expressions.case· Match a value against glob patterns and run the first matching branch; cleaner than long if/elif chains.- Bash Conditionals: if, [[ ]] and case · tutorial