Bash Script Broke on Filenames With Spaces or Asterisks (Unquoted `$var`)

Unquoted variable expansion undergoes word splitting and pathname globbing.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Bash Script Broke on Filenames With Spaces or Asterisks (Unquoted `$var`) Platform: Linux
Common Root Causes:
  • Unquoted variable expansion undergoes word splitting and pathname globbing.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Always double-quote parameter expansions (`"$var"`) and use arrays (`"${args[@]}"`) for argument lists.
Safe Diagnostic Command:
shellcheck -S warning <script.sh>
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Unquoted variable expansion undergoes word splitting and pathname globbing. Recommended diagnostic: `shellcheck -S warning <script.sh>`. Remediation: Always double-quote parameter expansions (`"$var"`) and use arrays (`"${args[@]}"`) for argument lists.

Worked Examples

  • Run `shellcheck -S warning <script.sh>` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-bash-unquoted-word-splitting-glob · Independently verified: 2026-11-25

Related Resources