Interview Prep/Shell Scripting/Troubleshooting: quoting, expansion and tool-input bugs

A script builds `OPTS="--delete --exclude=.git"` and runs `rsync "$OPTS" src/ dst/`. rsync fails with `rsync: --delete --exclude=.git: unknown option`.

AdvancedTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → Quoting the string passes all options as one argument. Removing the quotes "works" until an option value contains a space or a glob, then it breaks differently.

Diagnose → printf '[%s]\n' "$OPTS" shows a single argument.

Fix → Use an array, which keeps each option as a separate, intact argument:

opts=(--archive --delete --exclude='.git' --exclude='node_modules')
[[ $dry_run == 1 ]] && opts+=(--dry-run)
rsync "${opts[@]}" src/ dst/

Prevent → Never store command arguments in a plain string; arrays exist for exactly this.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: quoting, expansion and tool-input bugs