Use this when writing robust Bash scripts.
Bash Fundamentals
Bash fundamentals: scripts start with #!/usr/bin/env bash, use set -euo pipefail for safety, quote variables ("$var"), check exit codes and are made executable with chmod +x.
1. Fundamentals: shebang #!/usr/bin/env bash, set -euo pipefail, IFS, quoting.
2. Functions and local variables.
3. Check exit codes.
4. Readable and small.
5. Know POSIX sh vs bash differences.
script → bash interpreter → commands
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
First run bash -n, then execute controlled fixtures covering whitespace/glob characters, missing variables, command failure and pipeline failure. Capture the exact Bash version and environment, inspect exit status after each critical operation, and verify generated files or external target state independently. Where the script is intended to be idempotent, repeat it against the same fixture and compare state.
Step 2 Common mistakes Narrow the fault
- Missing
set -euo pipefail. - Using
shsyntax differences unknowingly.
Step 3 Where it fails Narrow the fault
Separate shell parse/expansion, local command availability/permissions, pipeline/redirection behavior, invoked tool semantics and remote target state. A quoting bug is different from an API rejection returned by curl; set -e behavior is not a transaction/rollback system. Do not add broad || true or disable error handling simply to continue. Fix the first failing shell/tool boundary and repeat the fixture.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Start with #!/usr/bin/env bash and set -euo pipefail.
- Quote every variable expansion: "$var".
- Check commands' exit codes.
head -3 script.sh— shebang and strict modeshellcheck script.sh— lintbash -x script.sh— trace
ReferenceWhy it matters1 section · depth on demand
In depth
Bash fundamentals: a script is a text file of shell commands, started with a shebang (#!/usr/bin/env bash) and made executable. Commands communicate through arguments, standard input/output and exit codes.
| Concept | Example |
|---|---|
| Arguments | $1, $@, $# |
| Command substitution | today=$(date +%F) |
| Pipes | One command's output feeds the next |
| Redirection | cmd > out.log 2>&1 |
| Test conditions | [[ -d /backup ]] |
| Exit status | $? |
chmod +x backup.sh && ./backup.sh /etc /backup
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
Bash automation is driven by parsing, expansions, quoting, redirections, pipelines, command substitution, functions and exit-status propagation. Text that looks simple can change meaning through word splitting or glob expansion when unquoted. Pipeline status normally follows shell rules rather than automatically surfacing every failed component. Subshells and command substitutions have their own execution/environment effects, so variable updates may not persist where an author expects.
ReferenceConfiguration & state1 section · depth on demand
Lifecycle
Treat shell scripts as versioned programs: use a clear interpreter requirement, explicit inputs/environment, dependency checks and predictable locale/path assumptions. Prefer functions and small commands, quote variable expansions, avoid parsing presentation output when a machine-readable interface exists, and keep secrets out of command arguments/logs. Use strictness options deliberately—understand their edge cases rather than pasting a boilerplate line.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Bash Automation > Bash Fundamentals. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%Quick lookup tables
Bash Automation → Bash Fundamentals · operational reference
Context-specific ownership, verification, output and failure-boundary lookup for Bash Automation → Bash Fundamentals.
| Layer | Bash Automation → Bash Fundamentals operational reference |
|---|---|
| Runtime/ownership | GNU Bash shell automation · identify controller/parser, durable intent, identity and target ownership before mutation |
| Persistent intent | Versioned definition/configuration with explicit dependencies, scope and credential boundary |
| Verification | bash -n script.sh && bash -u -o pipefail script.sh --fixture; printf 'exit=%s |
| Expected interpretation | The script parses cleanly, handles quoting/expansion intentionally, propagates pipeline/command failures according to policy, uses explicit inputs and exits with documented status. |
| Recovery boundary | Preserve correlation IDs + pre-state, repair first failing boundary, then verify the target independently |
Deep reference
Healthy vs suspicious evidence
Output interpretation
Capture script/revision, Bash version, sanitized arguments/environment, command or step context, stdout/stderr separately where useful, exit code and target identifiers. Do not treat printed “success” text as stronger than the actual exit status and post-state. For pipelines retain enough context to know which component failed when policy requires that distinction.