Наши партнеры

5.1. Quoting Variables

When referencing a variable, it is generally advisable to
enclose its name in double quotes.
This prevents reinterpretation of all special characters within
the quoted string -- except $, `
(backquote), and \ (escape).
[1]
Keeping $ as a special character within
double quotes permits referencing a quoted variable
("$variable"), that is, replacing the
variable with its value (see Example 4-1, above).

Use double quotes to prevent word splitting.
[2]
An argument enclosed in double quotes presents
itself as a single word, even if it contains whitespace separators.

List="one two three"
for a in $List # Splits the variable in parts at whitespace.
do
echo "$a"
done
# one
# two
# three
echo "---"
for a in "$List" # Preserves whitespace in a single variable.
do # ^ ^
echo "$a"
done
# one two three

Single quotes (' ') operate similarly to double
quotes, but do not permit referencing variables, since
the special meaning of $ is turned off.
Within single quotes, every special
character except ' gets interpreted literally.
Consider single quotes ("full quoting") to be a
stricter method of quoting than double quotes ("partial
quoting").

Since even the escape character (\)
gets a literal interpretation within single quotes, trying to
enclose a single quote within single quotes will not yield the
expected result.

Notes

Encapsulating "!" within double
quotes gives an error when used from the command
line. This is interpreted as a history command. Within a script,
though, this problem does not occur, since the Bash history
mechanism is disabled then.

Of more concern is the apparently
inconsistent behavior of \
within double quotes, and especially following an
echo -e command.