Quoting Arguments
Quotes help you get the desired degree of "verbatimicity" in your input.
From the source: GNU bash manual: Quoting.
Guidelines
Aggressively quoting text that does not need to be unquoted can improve the readability of scripts.
Use single quotes (') whenever possible
The bash interpreter will not have to apply expansions to text which does not require any, and other programmers will get syntax highlighting which clearly communicates: "This is just text".
Use double quotes (") if they are sufficient
When you need shell variables, command substitution, arithmetic, etc., use double quotes - the potential expansions are still a lot simpler than unquoted text.
Only use unquoted text when you absolutely need it
The bash interpreter (and other programmers even more so) have to assume you used unquoted text for a reason, which means there is something going on.
Mix single/double/no-quotes to further reduce expansions
Not quoting the entire text leaves you wide open to unintended shell expansions:
echo My \($USER\) HTML files: *.html
Use single or double quotes as much as possible, leaving the bare minimum subject to shell expansion:
echo 'My ('"$USER"') HTML files:' *.html
This also reduces the number of characters that have to be \-escaped.
Edit bash files with GNU Emacs
Emacs has excellent support for editing bash scripts, including syntax highlighting, hippie-expand, etc. Do not settle for less.
Processed input: Unquoted
Unquoted input is subjected to extensive Shell Expansions (preprocessing) before being passed on to commands:
echo <<input>>
Assuming <<input>> contain no quotes (neither ' nor "), these are examples of processing performed by the shell before the command is invoked:
Shell Expansion | Sample command | Output | Remarks |
---|---|---|---|
Remove comments |
echo abc # def |
abc |
Unquoted occurrences of # will cause the remainder of the line to be ignored. |
Word splitting |
echo a b c 1234 123 12 1 |
a b c 1 1 |
Unquoted redundant whitespace before, after, and within words will be collapsed into either one (within) or no (before and after) spaces. (the second line indicates whitespace occurrences in the first). The shell variable IFS is used to determine which characters should be considered "whitespace". |
Tilde ( ~) expansion |
|
|
If the expansion fails, the ~ is left unchanged. |
Filename Expansion |
|
Lists all files in the current directory:
|
|
Brace Expansion |
|
|
|
Shell Parameter Expansion | Let
myvar=something
|
|
|
Command Substitution |
|
|
|
Arithmetic Expansion |
echo $((2+2)) |
4 |
See also Arithmetic and the GNU bash manual: Shell Arithmetic. |
Quote Removal |
|
Both yield:
xxx |
Quotes that were not produced by any of the other expansions are removed. |
Escaping a character with special meaning
The characters ", ' and \ have special meanings. To get the character verbatim instead of the special meaning, prefix it by \:
echo \" # writes " to stdout echo \' # writes ' to stdout echo \\ # writes a single \ to stdout
One kind of quote included within the other will be output without special treatment:
echo "'" # writes ' to stdout echo '"' # writes " to stdout
An escaped " within "-s will be output without special treatment:
echo "\"" # writes " to stdout
See also the GNU bash manual: Escape Character.
Absolutely verbatim: single quotes (')
If your string is exactly like you want it, and the shell should just pass it on as is, use single quotes ( '):
echo 'No removal of # comments' echo 'No messing with whitespace: xxx' echo 'No tilde expansion: ~' echo 'No filename expansion: *' echo 'No brace expansion: {1..1000}' echo 'No shell variable expansion: $0 ${myvar}' echo 'No command substitution: $(ls)' echo 'No arithmetic expansion: $((2+2))' echo 'No "quote removal"!'
These strings are all passed on to the command exactly as they appear. All shell expansions are disabled (apart from the delimiting '-s being removed, of course). See also the GNU bash manual: Single Quotes.
Quite verbatim: double quotes (")
Double quoted strings are useful, but confusing: See the GNU bash manual: Double Quotes for a detailed description of how they work.
Some of the shell expansions are performed, and some are not:
- Enabled:
- Disabled:
-
- Remove comments
- Word splitting
- Tilde ( ~) expansion
- Filename Expansion
- Brace Expansion
Concerning Quote Removal, see Escaping a character with special meaning concerning how to retain quotes. See also the GNU bash manual: Quote Removal.