Double-quote to prevent word splitting and globbing
Unquoted variable expansions in Bash are split on whitespace and expanded as globs. SC2086 explained with examples and a safe fix.
Problem
When you write `$var` (or `$(cmd)`) without quotes, Bash performs two transformations on the result before passing it to a command: word splitting on whitespace (controlled by `IFS`) and pathname expansion (globbing on `*`, `?`, `[`). For a variable that holds `hello world` or a filename with a space, that means a single argument silently becomes two — or twenty. This is the single most common Bash bug. It hides until a path contains a space, a username has a hyphen interpreted as a flag, or an attacker controls input and slips in a glob. Almost every shell injection vulnerability traces back to an unquoted expansion.
Bad
#!/usr/bin/env bash
# DANGEROUS: $file is word-split and globbed
file=$1
rm $file # if $file is "my doc.txt" → two failed deletes
cp $src $dst # if $src is "*.conf" → matches every conf fileGood
#!/usr/bin/env bash
# Safe: each expansion is quoted
file=$1
rm -- "$file"
cp -- "$src" "$dst"Explanation
Double quotes suppress word splitting and globbing on the value, while still allowing variable and command substitution. The `--` end-of-options marker prevents filenames that start with `-` from being interpreted as flags. Use this pattern for every variable, every command substitution, and every positional parameter unless you have a specific reason to want splitting (very rare).
Related
SC2046
SC2068
SC2206