Pcornel
Linux · Shell & Bash

Shell Expansion

Cheat sheet · https://pcornel.dk/topic.php?topic=2682

Back to the Topic

Answer

Shell expansion is what Bash does to a command line before running it, in order: brace expansion, tilde, parameter/variable, command substitution, arithmetic, word splitting, then filename globbing, and finally quote removal.

Remember

Double quotes stop word splitting and globbing but allow $var and $(…); single quotes stop everything. Unquoted empty variables disappear, which can change a command's meaning (e.g. rm -rf $dir/).

First checks

  • bash -x script.sh — trace each expanded command
  • bash -n script.sh — syntax check only
  • shellcheck script.sh — common bugs and quoting problems
  • echo $? — exit code of the last command

Verify in this order

command line read → brace, tilde, parameter, command and arithmetic expansion → word splitting → globbing → quote removal → program receives argv

Where it usually breaks

First mismatch in persistent intent → provider/parser/generator → kernel/userspace runtime → dependency/security context → logs/counters → consumer-visible read-back; preserve identity/native diagnostic/pre-state before mutation.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=2682

Confirmation

Confirm action

Continue?