Control Flow
Core Idea
Control flow is how a shell program chooses what happens next.
Without control flow, a script is just a fixed list of commands. With control flow, scripts can branch on success, repeat work, and stop when checks fail.
Main Tools
- Conditionals: choose a branch with
if. - Loops: repeat commands with
fororwhile. - Exit status: communicate success or failure.
- One-liners: combine control flow at the prompt when it stays readable.
Example
if [[ -f ~/src/pages/index.md ]]; then
printf 'source exists\n'
else
printf 'missing source\n' >&2
exit 1
fi
Watch Out
Readable control flow beats clever control flow. If the command is hard to explain, make it a script with clear lines.
Docs Pointers
- Run
help if,help for, andhelp while. - Read if, for, while, and shell scripting.

Linux Foundations