Bash Shell Scripting
Bash reference covering quoting, parameters, arrays, tests, pipelines, functions, traps, files, processes, portability, security, debugging, and production-grade scripting.
Syntax, Expansion & Quoting
Options, Quoting & Expansion Order
Quote parameter expansions by default. Use arrays for argument lists and understand that shell parsing happens before commands receive arguments.
| Form | Expansion | Word Splitting / Glob | Use |
|---|---|---|---|
| '$value' | None | No | Literal text |
| "$value" | Parameter/command/arithmetic | No | Default variable use |
| $value | Parameter | Yes | Rare intentional splitting |
| "${array[@]}" | Each array element | No | Safe argument vector |
set -e has contextual exceptions. Handle expected failures explicitly and test pipelines, conditions, subshells, and cleanup paths.
Parameters, Defaults & Arrays
Use parameter expansion for required/default values and indexed/associative arrays to preserve argument boundaries.
Tests, Control Flow & Functions
[[ ]], Arithmetic, case & Loops
Prefer [[ ]] in Bash for strings/patterns, (( )) for arithmetic, case for dispatch, and read loops that preserve whitespace.
Functions, Exit Status & Traps
Functions return integer status, not rich values. Send data to stdout deliberately, diagnostics to stderr, and install cleanup traps early.
Before rm/mv/chmod, reject empty, root, home, unresolved, and unexpected paths. Prefer explicit paths over globs.
Processes, Pipelines & Files
Pipelines, Subshells & Background Jobs
Pipeline stages usually run in separate processes; pipefail surfaces earlier failures; capture PIDs and wait to own background work.
Files, find, NUL Delimiters & Portability
Use NUL delimiters for arbitrary filenames, -- before path operands, and select Bash versus POSIX sh deliberately.
Filenames may contain spaces, tabs, newlines, and leading dashes. Use globs, find -print0, or arrays.
Debugging, Security & Review
Tracing, ShellCheck & Tests
Use bash -n for syntax, ShellCheck for static issues, controlled xtrace for debugging, and automated tests for edge-case inputs and failure paths.
Production Checklist
Pin commands and formats, validate environment/input, avoid eval, preserve exit statuses, bound retries, and make reruns idempotent.
Quote expansions, use arrays, validate paths, create secure temp files, trap cleanup, avoid secret xtrace, check dependencies, use timeouts, lint, test, and document Bash-only features.
Recommended Next Sheets
Amazon Web Services (AWS)
AWS reference covering accounts, regions, IAM, networking, compute, storage, databases, serverless, messaging, observability, security, reliability, cost, infrastructure as code, and incident readiness.
Docker Containers & Compose
Beginner-to-advanced Docker reference covering images, containers, Dockerfiles, BuildKit, cache, lifecycle, storage, networking, Compose, registries, multi-platform builds, security, diagnostics, and production practices.
Git Modern Command Reference & Internal Architecture
High-density Git cheat sheet covering interactive staging, branch rebase workflows, reflog recovery, and visual branch architecture.