Why should you almost always quote variable expansions? Show what goes wrong.
BeginnerConceptVariables, quoting and expansions
Answer
Unquoted expansions undergo word splitting on IFS and pathname expansion.
file="monthly report.csv"
rm $file # rm: cannot remove 'monthly': No such file or directory
rm "$file" # correct
pattern="*"
echo $pattern # lists every file in the directory
dir=""
rm -rf $dir/* # becomes rm -rf /*
Quote "$var", "$(cmd)" and "${arr[@]}". The exceptions are deliberate: inside [[ ]], on the right side of an assignment, and when you intentionally want splitting (documented with a comment).
More on Variables, quoting and expansions
What are the rules for assigning variables in Bash?BeginnerWhat is the difference between single quotes, double quotes and `$'...'`?BeginnerExplain the difference between `"$@"`, `"$*"`, `$@` and `$*`.IntermediateWhat do `${var:-default}`, `${var:=default}`, `${var:+alt}` and `${var:?msg}` do?IntermediateHow do you strip prefixes and suffixes with parameter expansion?IntermediateHow do you do search-and-replace, substrings, length and case conversion without external tools?Intermediate