Quoting means just that, bracketing a string in quotes. This
has the effect of protecting special characters in the string from
reinterpretation or expansion by the shell or shell script. (A character
is "special" if it has an interpretation other than its
literal meaning, such as the wild card character,
*.)
When referencing a variable, it is generally advisable in
enclose it in double quotes (" "). This preserves
all special characters within the variable name, except
$, ', and \. This
allows referencing it, that is, replacing the variable with
its value (see Example 3-5, above). Enclosing the
arguments to an echo statement in double
quotes is usually a good practice (and sometimes required,
see Section 3.28).
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").
Escaping is a method of quoting single
characters. The escape (\)
preceding a character will either toggle on or turn off a special
meaning for that character, depending on context.
- \n
means newline
- \r
means return
- \t
means tab
- \v
means vertical tab
- \b
means backspace
- \a
means "alert" (beep or flash)
- \0xx
translates to the octal ASCII
equivalent of 0xx
# Use the -e option with 'echo' to print these.
echo -e "\v\v\v\v" # Prints 4 vertical tabs.
echo -e "\042" # Prints " (quote, ASCII character 42). |
- \"
gives the quote its literal meaning
echo "Hello" # Hello
echo "\"Hello\", he said." # "Hello", he said. |
- \$
gives the dollar sign its literal meaning
(variable name following \$ will not be
referenced)
echo "\$variable01" # results in $variable01 |
- \\
gives the backslash its literal meaning
The escape also provides a means of writing a
multi-line command. Normally, each separate line constitutes
a different command, but an escape at the end
of a line escapes the newline character,
and the command sequence continues onto the next line.
(cd /source/directory && tar cf - . ) | \
(cd /dest/directory && tar xvfp -)
# Repeating Alan Cox's directory tree copy command,
# but split into two lines for increased legibility. |