Bash Guidelines¶
Write Bash scripts that are idiomatic, robust, and readable. ShellCheck compliance is mandatory — run in CI.
Setup¶
Every script starts with:
-e— exit immediately on error-u— treat unset variables as errors-o pipefail— propagate errors through pipes
Variables¶
- Quote all expansions:
"$VAR","${VAR}". - Use
readonlyfor constants:readonly MAX_RETRIES=3. - Use
localfor all variables inside functions.
Functions¶
- Extract reusable logic into named functions.
- Define functions before they are called.
- Use explicit
return <code>; never rely on the exit code of the last statement.
Error Handling¶
- Print errors to
stderr:echo "Error: ..." >&2. - Use
trapfor cleanup:trap cleanup EXIT. - Check required commands exist:
command -v docker >/dev/null || { echo "docker not found" >&2; exit 1; }.
Style¶
- Prefer
[[ ]]over[ ]for conditionals. - Prefer
printfoverechofor portability. - Use
$(command)for command substitution, not backticks.
Unix Philosophy¶
- Silent on success; errors to
stderr. - Exit
0on success; non-zero on failure. - Do one thing; compose with pipes.
Directives¶
- Every script:
#!/usr/bin/env bash+set -euo pipefail - Quote all expansions:
"$VAR","${VAR}"; usereadonlyfor constants;localinside functions - Errors to stderr:
echo "Error: ..." >&2;trap cleanup EXITfor cleanup - Prefer
[[ ]]over[ ],printfoverecho,$(command)over backticks - Silent on success; non-zero exit on failure; ShellCheck compliance in CI