🐚 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

  1. Read sequentially for a structured path (01 → 15).
  2. Jump to a chapter as a reference when you hit a concept in the wild.
  3. Run the examples — the terminal is your REPL.
  4. Use ShellCheck on every script you write.
  5. 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 via brew install bash.
  • Command-line familiarity (ls, cd, cat, grep, sed, awk).
  • ShellCheck installed (brew install shellcheck or apt install shellcheck).

Curriculum

Part I — Shell Internals

#TopicWhat You'll Master
01Introduction & SetupShebang mechanics (execve/BINPRM_BUF_SIZE), set -euo pipefail internals, set -e exceptions, production script skeleton, trap ERR/trap EXIT, CRLF detection.
02Variables & Data TypesExpansion pipeline order (brace → tilde → param → arith → cmd → process → split → glob), word-splitting internals, IFS semantics, declare attributes, ${!var} indirect expansion.
03Operators & Expansions((...)) exit status quirk, [[]] vs [] mechanics, BASH_REMATCH capture groups, process substitution fds, glob mechanics (nullglob/failglob/extglob).
04Control FlowExit status as control flow, while IFS= read -r internals, pipe-into-while subshell trap, case glob dispatch, ;;&/;& fall-through, retry-with-backoff pattern.
05Functions & ScopeDynamic 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

#TopicWhat You'll Master
06Text Processinggrep 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.
07File System & I/OFile descriptor mechanics (fd 0-9), redirection order semantics, find -print0 + read -d '', mktemp race-free patterns, flock locking, atomic writes (temp + mv).
08Processes & Signals$!/wait mechanics, wait -n job pools, signal dispatch table, trap for graceful shutdown, timeout enforcement, xargs -P parallelism, nohup/disown/setsid.
09ArraysIndexed vs associative internals, [@] vs [*] word semantics, sparse arrays, mapfile -d '' for null-delimited I/O, nameref array passing, CSV processing.
10String ManipulationParameter expansion operator table, #/##/%/%% shortest/longest semantics, BASH_REMATCH capture groups, printf -v (no subprocess), regex ERE vs PCRE.

Part III — Production

#TopicWhat You'll Master
11Command-Line Argumentsgetopts state machine (OPTIND/OPTARG), manual long-option parsing, --flag=value extraction, subcommand dispatch, stdin detection ([[ -t 0 ]]).
12Debugging & Error Handlingset -e full exception model, local masking, inherit_errexit, trap ERR call-stack logging, exit code conventions (128+N), structured logging, dry-run pattern.
13Best PracticesProduction script architecture (main pattern, BASH_SOURCE[0] + symlink resolution), library sourcing with guards, readonly/local hygiene, eval avoidance, idempotent design.
14Testingbats test structure, run/$status/$output mechanics, bats-assert assertions, function mocking with export -f, parameterized tests, CI integration.
15Exercises & Projects7 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

Tooling

bash
# 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