Truth, the shell's way
In most languages, a condition is an expression that evaluates to a boolean. In the shell, a condition is a command, and "true" means the command succeeded. if grep -q error log.txt; then ... does not compute a boolean from grep's output; it runs grep, and takes the then branch if grep exited with status 0.
The same convention lets any program you install, written in any language, become a condition in a script: if it exits with 0, the shell treats the answer as yes. This design is the key to the shell's control flow, and it is also why the shell's syntax for tests looks so strange: [ is actually a command, whose name happens to be a bracket. Once you see that everything is "run a command, look at its exit status", the rest of this lesson falls into place: if, while, until, && and || all work the same way, and the four flavours of test are just different commands, or keywords, that produce a status.
Exit statuses and command lists
Definition— Exit status
A number from 0 to 255 that every command returns when it finishes. 0 means success; any other value means failure, and programs document what their non-zero values mean (grep returns 1 for "no match" and 2 for "error"; Bash uses 126 for "not executable" and 127 for "command not found"). The status of the last command is in $?.
Note the reversal from C and Python, where 0 is false. In the shell, there is one way to succeed and many ways to fail, so success gets the single value 0. The builtins true and false do nothing except return 0 and 1.
Commands can be combined by their statuses:
| Syntax | Meaning |
|---|---|
a ; b | run a, then b, regardless |
a && b | run b only if a succeeded |
a || b | run b only if a failed |
! a | invert a's status |
#!/usr/bin/env bash
true; echo "true gives $?"
false; echo "false gives $?"
printf 'alpha\nbeta\n' > words.txt
if grep -q beta words.txt; then # if tests a command's exit status
echo "found beta"
elif grep -q alpha words.txt; then
echo "found only alpha"
else
echo "found neither"
fi
grep -q gamma words.txt || echo "no gamma (|| runs on failure)"
mkdir -p build && cd build && echo "now in ${PWD##*/} (&& runs on success)"
if ! grep -q gamma ../words.txt; then echo "! inverts a status"; fitrue gives 0
false gives 1
found beta
no gamma (|| runs on failure)
now in build (&& runs on success)
! inverts a statusgrep -q ("quiet") prints nothing and only sets its status, which makes it the standard way to ask "does this file contain...?". mkdir -p build && cd build && ... is the idiomatic way to run a sequence of steps that each depend on the one before.
Four ways to test a condition
To compare strings or numbers or check files, you need a command that performs the comparison and returns a status. Bash offers four:
test exprand its synonym[ expr ]: the POSIX command.[is a builtin whose last argument must be]. Because it is an ordinary command, its arguments go through word splitting and globbing, so every variable inside it must be quoted.[[ expr ]]: a Bash keyword. The parser handles it specially, so variables inside it are not split or globbed, and it adds&&,||, pattern matching with==and regular expressions with=~. In Bash scripts, prefer it.(( expr )): arithmetic evaluation. It succeeds if the expression is non-zero, so(( n > 9 ))reads naturally. Variables need no$.- Any other command, as in the previous section.
#!/usr/bin/env bash
name="Ana Lima"
empty=""
n=12
touch notes.txt
[[ -n $name ]] && echo "name is not empty"
[[ -z $empty ]] && echo "empty has length zero"
[[ $name == Ana* ]] && echo "glob match: name starts with Ana"
if [[ $name =~ ^([A-Z][a-z]+)\ ([A-Z][a-z]+)$ ]]; then
echo "regex match: first=${BASH_REMATCH[1]} last=${BASH_REMATCH[2]}"
fi
(( n > 9 )) && echo "numerically, 12 > 9"
[[ 12 < 9 ]] && echo "but as strings, \"12\" < \"9\""
[ "$n" -gt 9 ] && echo "POSIX test: [ \"\$n\" -gt 9 ]"
[[ -f notes.txt && ! -d notes.txt ]] && echo "notes.txt is a regular file"
[[ -e missing.txt ]] || echo "missing.txt does not exist"name is not empty
empty has length zero
glob match: name starts with Ana
regex match: first=Ana last=Lima
numerically, 12 > 9
but as strings, "12" < "9"
POSIX test: [ "$n" -gt 9 ]
notes.txt is a regular file
missing.txt does not existThe operators you will use most:
| Test | Meaning |
|---|---|
-z "$s", -n "$s" | string is empty, or not empty |
"$a" == "$b", "$a" != "$b" | strings equal, or not (= also works) |
$s == pattern (in [[ ]]) | glob match, when the right side is unquoted |
$s =~ regex (in [[ ]]) | extended regular expression; captures in BASH_REMATCH |
-eq -ne -lt -le -gt -ge | integer comparisons in [ ] and [[ ]] |
(( a < b )) and friends | integer comparisons with ordinary operators |
-e path, -f path, -d path | exists; is a regular file; is a directory |
-r -w -x path | readable, writable, executable |
-s path | exists and is not empty |
a -nt b | file a is newer than file b |
The most common bug is mixing up string and number comparisons. Inside [[ ]], < and > compare strings in dictionary order, which is why "12" < "9" is true: the character 1 comes before 9. For numbers, use (( )) or -lt and friends. Another frequent one is quoting the right-hand side of == in [[ ]]: [[ $name == "Ana*" ]] compares with the literal text Ana*, while [[ $name == Ana* ]] is a pattern match. And put the regular expression of =~ in a variable, or leave it unquoted, because a quoted regex is matched as a literal string.
case: dispatching on patterns
When one value must be compared with many patterns, case is clearer than a chain of elifs. Each branch is a list of glob patterns separated by |, followed by ), commands, and ;;. The first matching branch runs; * at the end catches everything else.
#!/usr/bin/env bash
for file in report.pdf photo.JPG notes.txt backup.tar.gz Makefile data.csv; do
case ${file,,} in # match the lower-cased name against patterns
*.jpg | *.jpeg | *.png) kind="image" ;;
*.pdf | *.txt) kind="document" ;;
*.tar.gz | *.zip) kind="archive" ;;
*.*) kind="other: .${file##*.}" ;;
*) kind="no extension" ;;
esac
printf '%-14s %s\n' "$file" "$kind"
donereport.pdf document
photo.JPG image
notes.txt document
backup.tar.gz archive
Makefile no extension
data.csv other: .csvOrder matters: *.tar.gz must come before a general *.* would match it, and Makefile reaches the final * because it has no dot. Lower-casing the subject with ${file,,} made the patterns case-insensitive. case is the standard way to parse a script's subcommands and options (start | stop | restart), and lesson 5 uses it for exactly that. Two variants of ;; exist since Bash 4.0: ;& falls through to the next branch, and ;;& continues testing the remaining patterns.
Loops
Bash has three loops, plus a C-style form of for:
for name in words; do ...; doneruns once per word after expansion: a literal list, a glob (*.csv), an array ("${items[@]}") or the script's arguments ("$@", which is also the default if you write justfor name; do).for (( init; condition; step ))counts with arithmetic.while command; do ...; donerepeats while the command succeeds;until commandrepeats while it fails.breakleaves the loop andcontinueskips to the next round; both accept a number to affect enclosing loops.
#!/usr/bin/env bash
for (( i = 3; i >= 1; i-- )); do # C-style for loop
printf '%d... ' "$i"
done
echo "liftoff"
n=0
while true; do
(( ++n ))
(( n % 2 == 0 )) && continue # skip even numbers
(( n > 9 )) && break # leave the loop
printf '%d ' "$n"
done
echo
attempt=0
until [[ -f ready.flag ]]; do # until: loop while the test fails
(( ++attempt ))
echo "attempt $attempt: not ready yet"
if (( attempt == 3 )); then touch ready.flag; fi
done
echo "ready after $attempt attempts"
colours=(red "light green" blue)
for c in "${colours[@]}"; do printf '<%s> ' "$c"; done; echo
for word in $(printf 'one two'); do printf '[%s] ' "$word"; done; echo # deliberately unquoted: split into words3... 2... 1... liftoff
1 3 5 7 9
attempt 1: not ready yet
attempt 2: not ready yet
attempt 3: not ready yet
ready after 3 attempts
<red> <light green> <blue>
[one] [two] The last two lines contrast the right and the wrong way to loop over data. "${colours[@]}" gives one round per element, keeping light green together. An unquoted $(...) is split into words at every space, which is sometimes what you want (and then deserves a comment) but is wrong for anything like file names or lines of text. To loop over lines, use while IFS= read -r from lesson 3; to loop over files, use a glob directly (for f in *.log), never the output of ls.
Counting loops have several spellings. for i in {1..10} is the shortest when the bounds are fixed; for (( i = 1; i <= n; i++ )) works when they are in variables; seq (for i in $(seq 1 "$n")) is common in older scripts and on systems without Bash. And for interactive menus, Bash has a fourth loop, select, which prints a numbered list of choices, reads the user's pick into a variable and repeats until you break. It is handy in small setup scripts that a person runs by hand, and useless in automation, which should never wait for keyboard input.
Putting it together: a disk check
Here is a small but realistic script of the kind that runs from a scheduler on servers: it reads disk usage figures (fixed text here; in real life they would come from df), classifies each one, skips readings it cannot understand without crashing, and finishes with an exit status that says how bad things are, so that a monitoring system can act on it:
#!/usr/bin/env bash
# Classify disk usage readings and exit non-zero if anything is critical.
readings='/ 71%
/home 93%
/var 88%
/boot oops
/data 42%'
warn=80 crit=90 worst=0
while read -r mount used; do
pct=${used%\%} # strip the trailing %
if [[ ! $pct =~ ^[0-9]+$ ]]; then
printf '%-6s ?? unreadable value %q\n' "$mount" "$used"
continue
fi
if (( pct >= crit )); then
level=CRIT; (( worst < 2 )) && worst=2
elif (( pct >= warn )); then
level=WARN; (( worst < 1 )) && worst=1
else
level=ok
fi
printf '%-6s %-4s %3d%%\n' "$mount" "$level" "$pct"
done <<< "$readings"
case $worst in
0) echo "all good" ;;
1) echo "some disks need attention" ;;
2) echo "critical: act now" ;;
esac
echo "would exit with status $worst"/ ok 71%
/home CRIT 93%
/var WARN 88%
/boot ?? unreadable value oops
/data ok 42%
critical: act now
would exit with status 2Every construct in this lesson appears: a while read loop fed by a here-string, parameter expansion to strip the %, a regular-expression test to reject garbage, continue to skip it, an if/elif/else chain on numbers with (( )), && for a one-line update, and a case on the final result. The convention of 0 for OK, 1 for warning and 2 for critical is the one used by Nagios-style monitoring checks, which is why the script tracks the worst level seen rather than stopping at the first problem. A real version would end with exit "$worst" instead of the last echo. Notice also printf '%q', which printed the bad value in a form that is safe to paste back into a shell; it is invaluable in error messages about untrusted input.
Habits of reliable conditions
A few habits, each learned the hard way by many people:
- In Bash scripts, use
[[ ]]for strings and files and(( ))for numbers. Use[ ]only in scripts that must run under plainsh, and then quote everything inside it. - Test commands directly. Write
if grep -q x file; then, notgrep -q x file; if [ $? -eq 0 ]; then. The direct form is shorter and cannot be broken by a line inserted in between. - Do not use
a && b || cas if-then-else. Ifbitself fails,cruns too. It is fine for simple cases wherebcannot fail (such asecho), but anifstatement says what you mean. - Always handle the "else" case of a
case. A final*)branch that reports an unexpected value turns silent misbehaviour into a clear error message. - Prefer positive, named conditions. A function such as
is_weekend() { [[ $(date +%u) -ge 6 ]]; }makes the calling code read like a sentence,if is_weekend; then, and its status is simply that of its last command. - Let exit statuses mean something in your own scripts. End with
exit 0on success and a non-zero status on failure, so that your script can itself be used in anif, a&&chain or a CI pipeline.
Common misconceptions
Misconception
"[ is part of the if syntax." if takes any command; [ is just a command named [. That is why the spaces are mandatory: if [$x == 1] tries to run a command called [$x. Write if [ "$x" = 1 ], or better, if [[ $x == 1 ]].
Misconception
"[ -n $var ] checks that var is non-empty." If var is empty and unquoted, the command becomes [ -n ], a test with a single argument, which is true because -n is a non-empty string. Quote it, [ -n "$var" ], or use [[ -n $var ]], where the parser keeps the empty argument.
Misconception
"0 means false." In the shell, 0 means success, which if treats as true. Inside (( )), arithmetic follows C (0 is false), and the command's status is then 1. Both are consistent once you remember that (( )) is a command reporting whether its expression was non-zero.
Misconception
"[[ $a > $b ]] compares numbers." It compares strings. [[ 100 > 20 ]] is false. Use (( a > b )).
Misconception
"for line in $(cat file) loops over lines." It loops over words, and expands any wildcards in the file's text. Use while IFS= read -r line; do ...; done < file.
Exercises
Predict the status or output first, then check your answer with bash.
1. Exit statuses, conditionals and loops
Warm-upComputational/dev/null is always empty. What does this print?
grep -q x /dev/null
echo $?
2. Exit statuses, conditionals and loops
StretchComputationalWhich letters does this print? Give each letter that appears.
v=""
[ -n $v ] && echo A
[[ -n $v ]] && echo B
3. Exit statuses, conditionals and loops
CoreComputationalWhat does this print?
[[ 100 > 20 ]] && echo yes || echo no
4. Exit statuses, conditionals and loops
CoreComputationalWhat does this print?
(( 0 ))
echo $?
5. Exit statuses, conditionals and loops
CoreComputationalWhat does this print?
for i in 1 2 3 4 5 6; do
(( i % 3 == 0 )) && continue
(( i > 5 )) && break
printf '%d' "$i"
done
echo
6. Exit statuses, conditionals and loops
CoreComputationalWhat does true && false || echo fallback print, and why?
7. Exit statuses, conditionals and loops
Warm-upComputationalWhat does this print?
n=0
until (( n >= 4 )); do
(( n += 3 ))
done
echo "$n"