Quoting

View saved

Why quoting matters

Unquoted expansions split on whitespace and may expand globs. Most bugs in beginner scripts come from missing quotes.

file="My Notes.txt"
# Broken if unquoted:
# cat $file
cat "$file"

Single vs double

Single quotes preserve literals. Double quotes still expand $vars and $(...).

echo 'Path is $HOME'
echo "Path is $HOME"

Command substitution

Prefer $(...) over backticks, and quote the result when it is one word.

today="$(date +%F)"
echo "$today"

When you want splitting

Sometimes you intentionally leave a variable unquoted to split into arguments—do it deliberately, not by accident.

Comments

One comment per signed-in account. Comments are saved with this page’s URL.