Use this when Bash rewrites commands before running them.
Shell Expansion
Shell expansion is how Bash rewrites a command before running it: variables, globs like *.log, brace expansion {a,b}, tilde and arithmetic $(( )). Quoting controls which expansions happen.
1. Expansion order: brace, tilde, parameter, command, arithmetic, word splitting, globbing.
2. Quotes prevent splitting/globbing.
3. nullglob/failglob for globs.
4. Parameter expansion for string ops.
5. Be explicit.
text → expansions → final words
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 Common mistakes Narrow the fault
- Unquoted
$varbreaking on spaces. - A glob with no matches staying literal (use
shopt -s nullglob).
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.
- Bash expands variables, globs, braces, tilde and arithmetic before running.
- Double quotes stop word splitting and globbing.
- Unmatched globs stay literal unless nullglob is set.
echo {1..3}— brace expansionshopt -s nullglob— empty glob handlingecho "${var//old/new}"— parameter expansion
ReferenceWhy it matters1 section · depth on demand
What it is
Bash expands words before running a command: variables ($x), globs (*.cfg), braces ({a,b}), tilde (~) and arithmetic ($((1+2))). Quote variables to stop word splitting and glob expansion.
ReferenceImportant terms & boundaries2 sections · depth on demand
How it works
Bash expands a command line in a fixed order: brace expansion, tilde, parameter/variable, arithmetic, command substitution, word splitting, then pathname (glob) expansion, and finally quote removal. Double quotes stop word splitting and globbing but still allow $var and $(...); single quotes stop everything.
Example
for f in backups/*.cfg; do echo "$f"; done
mkdir -p site/{oslo,bergen}
echo $((count + 1))
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 > Shell Expansion. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%