Debugging
Bash · Reference cheat sheet
Debugging
Bash · Reference cheat sheet
📋 Overview
Debug bash with tracing (set -x), verbose mode, ShellCheck, and strategic logging. Reproduce with the same options and environment as production. Prefer small reproducible snippets over editing large scripts blindly.
🔧 Core concepts
| Tool | Purpose |
|---|---|
set -x / set +x | Trace expanded commands |
set -v | Print input lines as read |
PS4 | Prefix for set -x traces |
bash -n script | Syntax check only |
bash -x script | Run with trace |
trap | Catch ERR / DEBUG / EXIT |
| ShellCheck | Lint common bugs |
printf '%q\n' | Show shell-escaped values |
ERR trap runs when a command fails (with set -e nuances). DEBUG trap runs before each command.
💡 Examples
Trace with context:
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
set -x
# … suspect code …
set +xERR trap:
trap 'echo "ERR at ${BASH_SOURCE}:${LINENO}: $BASH_COMMAND" >&2' ERR
set -euo pipefailInspect values:
declare -p var
printf 'args:'; printf ' <%q>' "$@"; echo
type -a cmd
command -v cmdDry syntax + lint:
bash -n ./deploy.sh
shellcheck -x ./deploy.shConditional debug flag:
debug() { [[ "${DEBUG:-}" == 1 ]] && printf '%s\n' "$*" >&2; }
debug "port=$port"⚠️ Pitfalls
set -xprints secrets—disable around credential handling or redact.- Tracing changes timing (Heisenbugs in races).
bash -ndoes not catch runtime logic errors.- Don’t leave
set -xon in production cron output floods. - Pipeline failures need
pipefailto surface in traces meaningfully. - Subshells complicate stack traces—note
(…)boundaries.