Use it when a command gets different arguments than you expected — spaces in filenames, empty variables or a * that did not match.
Shell Expansion
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.
1. Print what the command really receives: prefix it with printf '%s\n'.
2. set -x — see the expanded command.
3. Spaces or wildcards in values → quote: "$var".
4. Empty variable → guard with ${var:?not set}.
5. Re-run and compare.
command line read → brace, tilde, parameter, command and arithmetic expansion → word splitting → globbing → quote removal → program receives argv
For Shell & Bash → Shell Expansion, continue with the first unproven Topic check: Identify all expansion forms in the command and record relevant shell options plus IFS. If that boundary is healthy, advance to the adjacent check: Use safe diagnostic techniques to inspect final argument boundaries rather than relying on echo formatting. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.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 Operational evidence path Verify boundary
parsed word → brace/tilde/parameter/arithmetic/command expansion → word splitting → pathname expansion → quote removal → final argv/redirection target → command behavior
Healthy evidence: final argv boundaries and redirection targets match intent across empty, whitespace and wildcard-containing inputs under the target shell options.
Step 2 Verification and output interpretation Verify boundary
Existing fast checks for this Topic. - Identify all expansion forms in the command and record relevant shell options plus IFS.
- Use safe diagnostic techniques to inspect final argument boundaries rather than relying on echo formatting.
- Quote expansions unless deliberate splitting/globbing is required and validate array semantics separately.
- For mutating operations, test expansion against fixtures containing whitespace, wildcard characters, empty values and leading dashes.
Verification chain. parsed word → brace/tilde/parameter/arithmetic/command expansion → word splitting → pathname expansion → quote removal → final argv/redirection target → command behavior
Step 3 Commands Verify boundary
echo {1..5}
echo ~
echo ${HOME}
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: unquoted data is split/globbed, empty arguments disappear, or shell options change unmatched-pattern behavior and target selection.
Step 5 Common mistakes Narrow the fault
- Unquoted expansions splitting on spaces.
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.
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/).
bash -x script.sh— trace each expanded commandbash -n script.sh— syntax check onlyshellcheck script.sh— common bugs and quoting problemsecho $?— exit code of the last command
ReferenceWhat it is1 section · depth on demand
Core model
Trace expansions in Bash evaluation order and pay special attention to quoting. The same variable or command substitution can yield one argument, many arguments or zero pathname matches depending on quotes, IFS and glob options; inspect final argv before allowing a mutating command to consume it.
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
Expansion verification shows final word/argument formation for the captured shell state and inputs, but it does not show the called program interprets those arguments safely.
In depth
The order of shell expansions
Before a command runs, Bash transforms the command line through a fixed series of expansions. The order explains many results that look odd at first. The Bash reference manual gives the sequence:
1. Brace expansion.
2. Tilde expansion, parameter and variable expansion, arithmetic expansion and command substitution, from left to right (and process substitution, where supported).
3. Word splitting.
4. Filename expansion (globbing).
Quote removal happens after all of them. Only brace expansion, word splitting and filename expansion can change the number of words, except for "$@" and "${name[@]}".
Each step with an example
| Step | Input | Becomes |
|---|---|---|
| Brace | backup-{a,b}.tar | backup-a.tar backup-b.tar |
| Tilde | ~/notes | /home/alice/notes |
| Parameter | $HOME, ${USER:-nobody} | Variable value or default |
| Arithmetic | $((2 * 512)) | 1024 |
| Command substitution | $(date +%F) | 2026-11-28 |
| Word splitting | Unquoted results | Split on IFS characters |
| Filename expansion | *.log | Matching file names |
| Quote removal | "a b" | a b (one word) |
Consequences of the order
| Behaviour | Why |
|---|---|
echo {1..$n} doesn't expand to a range | Brace expansion happens before $n is replaced |
v='*.txt'; ls $v lists files | Globbing runs after variable expansion, on unquoted results |
"$v" prevents both splitting and globbing | Quoted results skip word splitting and filename expansion |
~ inside quotes isn't expanded | Tilde expansion only applies to unquoted tildes |
host$ echo file{1..3}.txt
file1.txt file2.txt file3.txt
host$ n=3; echo {1..$n}
{1..3}
host$ seq 1 "$n" | paste -sd' '
1 2 3
host$ echo "Today: $(date +%F), disk: $(( $(df --output=avail / | tail -1) / 1024 )) MiB free"
host$ echo "~" ~
~ /home/alice
host$ set -x; ls *.conf; set +x
+ ls app.conf db.conf
Seeing the result
set -x prints each command after all expansions, just before it runs. It's the quickest way to see what a line really turned into, as the last example shows.
Documented example, not captured output.
How it works at runtime
Shell Expansion is part of Shell & Bash → Shell Expansion. Shell Expansion is the transformation phase that turns parsed shell words into final arguments and redirection targets through parameter, command, arithmetic, tilde, brace, pathname and word-splitting behavior.
Topic-specific state model. Shell Expansion is the transformation phase that turns parsed shell words into final arguments and redirection targets through parameter, command, arithmetic, tilde, brace, pathname and word-splitting behavior.
Interpretation rule. - Variable text ≠ one argument when unquoted word splitting or pathname expansion can occur.
- Quoted "$var" ≠ unquoted $var.
- Expansion result ≠ literal source text; nested command/parameter/arithmetic expansions can alter it before execution.
- An unmatched glob ≠ always a literal pattern; nullglob/failglob options change the result.
Quick reference
Bash expands a command line in this order: brace expansion → tilde → parameter/variable, arithmetic and command substitution → word splitting → filename (glob) expansion → quote removal.
echo {1..3}-{a,b} # 1-a 1-b 2-a …
echo ~alice # /home/alice
echo $((2**10)) # 1024
files="a b"; touch $files # two files (word splitting)
touch "$files" # one file named 'a b'
Most script bugs come from unquoted variables undergoing word splitting and globbing — quote "$var" unless you explicitly want splitting. set -x shows expanded commands.
Limits and how it fails
Primary failure point. Expansion verification shows final word/argument formation for the captured shell state and inputs, but it does not show the called program interprets those arguments safely.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Distribution/platform context. Globbing and Command Substitution own their specific expansion mechanisms; Variables owns data/storage semantics; this Topic owns the combined expansion pipeline.
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 Shell & Bash → 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%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
Shell Expansion operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for Shell Expansion.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | Shell Expansion is the transformation phase that turns parsed shell words into final arguments and redirection targets through parameter, command, arithmetic, tilde, brace, pathname and word-splitting behavior. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin host/distribution + shell/version/mode → identify cwd/mount namespace + input/path/stream → preserve quoting/expansion and final argv → map fd/pipeline/process boundaries → inspect object/variable/status/signal state → make one minimal reversible change → read back effective state → repeat original consumer/script/workload test. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | parsed word → brace/tilde/parameter/arithmetic/command expansion → word splitting → pathname expansion → quote removal → final argv/redirection target → command behavior | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | Expansion verification proves final word/argument formation for the captured shell state and inputs; it does not prove the called program interprets those arguments safely. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux file/text/shell evidence can establish this proof path: parsed word → brace/tilde/parameter/arithmetic/command expansion → word splitting → pathname expansion → quote removal → final argv/redirection target → command behavior | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove behavior outside the captured host, shell/session, cwd/namespace, input, process, filesystem, locale, time or workload scope. Expansion verification proves final word/argument formation for the captured shell state and inputs; it does not prove the called program interprets those arguments safely. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven boundary: capture the exact shell-delivered argv/streams or resolved object, verify the effective target state, change only the demonstrated cause, then repeat the original consumer/script/workload and adjacent regression checks. | Continue to the next unresolved boundary while preserving the original test condition. |
Shell Expansion · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | Shell Expansion |
|---|---|
| Topic path | Shell & Bash → Shell Expansion |
| Runtime state | Shell Expansion is the transformation phase that turns parsed shell words into final arguments and redirection targets through parameter, command, arithmetic, tilde, brace, pathname and word-splitting behavior. |
| Fast checks | - Identify all expansion forms in the command and record relevant shell options plus IFS. - Use safe diagnostic techniques to inspect final argument boundaries rather than relying on echo formatting. - Quote expansions unless deliberate splitting/globbing is required and validate array semantics separately. - For mutating operations, test expansion against fixtures containing whitespace, wildcard characters, empty values and leading dashes. |
| Verification chain | parsed word → brace/tilde/parameter/arithmetic/command expansion → word splitting → pathname expansion → quote removal → final argv/redirection target → command behavior Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return path or regression → explicit Evidence boundary. |
| Failure boundary | Expansion verification proves final word/argument formation for the captured shell state and inputs; it does not prove the called program interprets those arguments safely. Boundary chain: process/context → userspace configuration → kernel subsystem/resource → device/network/filesystem dependency → effective state → original workload. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |