🐚 Bash — Engineering Deep-Dive
A code-first, engineering-grade Bash curriculum. Each chapter is a deep-dive reference — minimal prose, maximum annotated production code, anti-patterns with fixes, edge cases, and under-the-hood commentary. Designed for mid-level developers moving to senior roles who learn through rigorous, real-world code.
How to Use This Reference
- Read sequentially for a structured path (01 → 15).
- Jump to a chapter as a reference when you hit a concept in the wild.
- Run the examples — the terminal is your REPL.
- Use ShellCheck on every script you write.
- Study the anti-patterns — the "❌ NAIVE → ✅ CORRECT" pairs are the core of this curriculum.
Prerequisites
- A Unix-like system (Linux, macOS, or WSL on Windows).
- Bash 4+ recommended (associative arrays,
mapfile,${var^^}case conversion). macOS ships 3.2 — install Bash 5 viabrew install bash. - Command-line familiarity (
ls,cd,cat,grep,sed,awk). - ShellCheck installed (
brew install shellcheckorapt install shellcheck).
Curriculum
Part I — Shell Internals
| # | Topic | What You'll Master |
|---|---|---|
| 01 | Introduction & Setup | Shebang mechanics (execve/BINPRM_BUF_SIZE), set -euo pipefail internals, set -e exceptions, production script skeleton, trap ERR/trap EXIT, CRLF detection. |
| 02 | Variables & Data Types | Expansion pipeline order (brace → tilde → param → arith → cmd → process → split → glob), word-splitting internals, IFS semantics, declare attributes, ${!var} indirect expansion. |
| 03 | Operators & Expansions | ((...)) exit status quirk, [[]] vs [] mechanics, BASH_REMATCH capture groups, process substitution fds, glob mechanics (nullglob/failglob/extglob). |
| 04 | Control Flow | Exit status as control flow, while IFS= read -r internals, pipe-into-while subshell trap, case glob dispatch, ;;&/;& fall-through, retry-with-backoff pattern. |
| 05 | Functions & Scope | Dynamic scoping internals (FUNCNAME/BASH_SOURCE/BASH_LINENO), local masks set -e, nameref (local -n) mechanics, signal traps for graceful shutdown, export -f for child processes. |
Part II — Core Systems
| # | Topic | What You'll Master |
|---|---|---|
| 06 | Text Processing | grep BRE/ERE/PCRE modes, sed -i GNU vs BSD breakage, awk as a mini language (BEGIN/END, associative arrays), pipeline composition for log analysis, LC_ALL=C determinism. |
| 07 | File System & I/O | File descriptor mechanics (fd 0-9), redirection order semantics, find -print0 + read -d '', mktemp race-free patterns, flock locking, atomic writes (temp + mv). |
| 08 | Processes & Signals | $!/wait mechanics, wait -n job pools, signal dispatch table, trap for graceful shutdown, timeout enforcement, xargs -P parallelism, nohup/disown/setsid. |
| 09 | Arrays | Indexed vs associative internals, [@] vs [*] word semantics, sparse arrays, mapfile -d '' for null-delimited I/O, nameref array passing, CSV processing. |
| 10 | String Manipulation | Parameter expansion operator table, #/##/%/%% shortest/longest semantics, BASH_REMATCH capture groups, printf -v (no subprocess), regex ERE vs PCRE. |
Part III — Production
| # | Topic | What You'll Master |
|---|---|---|
| 11 | Command-Line Arguments | getopts state machine (OPTIND/OPTARG), manual long-option parsing, --flag=value extraction, subcommand dispatch, stdin detection ([[ -t 0 ]]). |
| 12 | Debugging & Error Handling | set -e full exception model, local masking, inherit_errexit, trap ERR call-stack logging, exit code conventions (128+N), structured logging, dry-run pattern. |
| 13 | Best Practices | Production script architecture (main pattern, BASH_SOURCE[0] + symlink resolution), library sourcing with guards, readonly/local hygiene, eval avoidance, idempotent design. |
| 14 | Testing | bats test structure, run/$status/$output mechanics, bats-assert assertions, function mocking with export -f, parameterized tests, CI integration. |
| 15 | Exercises & Projects | 7 production projects: backup with retention, log analyzer, pre-commit hook, process supervisor with backoff, parallel image resizer, dotfile manager, capstone deployment script. |
Learning Path Suggestions
If you're a developer using Bash for CI/CD
Read 01 (strict mode skeleton), 06 (log analysis pipelines), 07 (file I/O, find -print0), 08 (processes, timeout, wait), 12 (error handling architecture), 15 (deployment capstone).
If you're a sysadmin
Read 06 (text tools for log parsing), 07 (file system, permissions, locking), 08 (processes, signals, graceful shutdown), 11 (CLI parsing), 13 (production script architecture), 15 (backup, supervisor, deploy).
If you know Bash but scripts keep breaking
Read 01 (set -euo pipefail internals — why your script silently fails), 02 (quoting/IFS/word-splitting — the #1 bug source), 05 (local masks set -e — the #2 bug source), 12 (error handling, trap ERR), 13 (production architecture).
If you want to write testable, maintainable Bash
Read 13 (script architecture, main pattern, library sourcing), 14 (bats testing, mocking), 15 (production projects with tests), then apply the checklist at the end of chapter 15.
Companion Resources
- GNU Bash Manual — the definitive reference.
- ShellCheck — the Bash linter. Install locally for real-time linting.
- Bash Hackers Wiki — idioms and gotchas.
- Pure Bash Bible — Bash-only replacements for external tools.
- explainshell.com — paste a command, see what each part does.
Tooling
# VS Code extensions:
# - "ShellCheck" (timonwong.shellcheck) — linting
# - "Bash IDE" (mads-hartmann.bash-ide) — language server
# - "Shell-format" (foxundermoon.shell-format) — formatter
# Install ShellCheck:
brew install shellcheck # macOS
apt install shellcheck # Debian/Ubuntu
# Install bats-core (testing):
brew install bats-core # macOS
npm install -g bats # npm
# Install GNU coreutils (macOS — for gcp, gmv, gsed, etc.):
brew install coreutils gnu-sed