Bash internals and implementation boundaries
Distinguish three evidence levels: documented language behavior, observed runtime behavior in a recorded environment, and implementation details tied to a specific source revision. A source tree can help explain parser and executor decisions, but its file names, functions and optimizations are not public language contracts. This batch does not inspect a pinned Bash source tree. Instead, it uses reproducible probes. type and hash describe observable command lookup state; BASH_CMDS exposes an associative view of the command hash table in Bash, and BASH_ALIASES exposes alias entries. Treat them as read-only inspection tools here, even though adding entries can affect shell state. Unsetting these special arrays removes their special properties; avoid that in normal scripts. command_not_found_handle, when defined, is called in a separate execution environment for unsuccessful ordinary command lookup and can supply its status. It is not a universal hook for every failed command. Bash debugging variables and tracing describe execution at the shell level, not syscall order. A conceptual model (parse → expand → redirect → execute → collect status) guides reasoning but omits context-specific ordering and implementation optimizations. For a source audit, pin the exact upstream revision and distribution patches, identify the documented rule, reproduce behavior, inspect the relevant code paths and separate inference from observation. The command-not-found example deliberately uses a synthetic absent command and checks the handler status. A command that exists but fails during execution is not handled by this lookup hook.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Inspect command hash state
hash -r || exit 1hash cat || exit 1[[ -n ${BASH_CMDS[cat]} ]] && printf 'cached\n'hash -r || exit 1[[ ! -v 'BASH_CMDS[cat]' ]] && printf 'cleared\n' Expected stdout:
cachedclearedThe probe checks the public view of the cache without assuming an absolute executable path.
Inspect alias metadata
alias bm_probe='printf ready'printf '%s\n' "${BASH_ALIASES[bm_probe]}"unalias bm_probe[[ ! -v 'BASH_ALIASES[bm_probe]' ]] && printf 'removed\n' Expected stdout:
printf readyremovedThe array reflects alias state; inspection does not execute the alias.
Observe lookup handler status
marker=parentcommand_not_found_handle() { marker=child printf 'missing=%s\n' "$1" return 42}bm_missing_command_8f12rc=$?[[ $rc == 42 && $marker == parent ]] || exit 1printf 'status=%s marker=%s\n' "$rc" "$marker"Expected stdout:
missing=bm_missing_command_8f12status=42 marker=parentThe handler runs in a separate execution environment and supplies status 42.
Common mistakes
A successful probe is not a universal proof or a source audit. Do not equate xtrace with system-call tracing.
Practice
Choose a parser or hash behavior. Write a claim, a minimal reproducer and an expected observation. Record interpreter version and propose a source-review plan without inventing code locations.
Next: Defensive scripting and security