Quoting: Double, Single, None
Quoting controls shell expansion. Single quotes preserve literal text; double quotes allow selected expansions while suppressing word splitting and filename expansion for ordinary parameter values.
Compare the exact arguments produced by quoted and unquoted expansions.
A student renames her folder to My Notes, runs backup.sh, and tar replies that /home/asha/My and Notes do not exist. The script did not change; the data did. Quoting is how you tell Bash where one piece of text ends, and it is the single biggest source of shell bugs.
The rule
Double quotes keep
$alive; single quotes keep everything literal.
In Bash, every value is a string. What changes is how Bash treats the text on the way into a command:
| Written as | Variables expand? | Split on spaces? | * matches files? |
|---|---|---|---|
$src |
yes | yes | yes |
"$src" |
yes | no | no |
'$src' |
no | no | no |
src="$HOME/My Notes"
echo '$src' # prints: $src
echo "$src" # prints: /home/asha/My Notes
ls $src # ls receives two arguments: /home/asha/My and Notes
ls "$src" # ls receives one argument, space includedInside double quotes, a few characters stay special: $, backquote, and \. Write \$ for a literal dollar and \" for a literal double quote. Inside single quotes nothing is special, not even the backslash, so a single quote cannot appear inside single quotes at all. For text like It's done, use double quotes.