简而言之,在不需要 shell 执行分词和通配符扩展的地方引用所有内容。
单引号逐字保护它们之间的文本。当您需要确保外壳完全不接触琴弦时,它是合适的工具。通常,当您不需要变量插值时,它是首选的引用机制。
$ echo 'Nothing \t in here $will change'
Nothing \t in here $will change
$ grep -F '@&$*!!' file /dev/null
file:I can't get this @&$*!! quoting right.
双引号适用于需要变量插值的情况。通过适当的调整,当您需要在字符串中使用单引号时,它也是一个很好的解决方法。 (没有直接的方法可以在单引号之间转义单引号,因为单引号内没有转义机制——如果有,它们不会完全逐字引用。)
$ echo "There is no place like '$HOME'"
There is no place like '/home/me'
当您特别需要 shell 执行分词和/或通配符扩展时,不适合使用引号。
Word splitting(又名令牌拆分);
$ words="foo bar baz"
$ for word in $words; do
> echo "$word"
> done
foo
bar
baz
相比之下:
$ for word in "$words"; do echo "$word"; done
foo bar baz
(循环只运行一次,在单个带引号的字符串上。)
$ for word in '$words'; do echo "$word"; done
$words
(循环仅在文字单引号字符串上运行一次。)
通配符扩展:
$ pattern='file*.txt'
$ ls $pattern
file1.txt file_other.txt
相比之下:
$ ls "$pattern"
ls: cannot access file*.txt: No such file or directory
(没有直接命名为file*.txt的文件。)
$ ls '$pattern'
ls: cannot access $pattern: No such file or directory
(也没有名为$pattern的文件!)
在更具体的术语中,任何包含文件名的内容通常都应该被引用(因为文件名可以包含空格和其他 shell 元字符)。通常应该引用包含 URL 的任何内容(因为许多 URL 包含 shell 元字符,例如 ? 和 &)。通常应引用任何包含正则表达式的内容(同上)。除非空白字符之间的单个空格外,任何包含重要空白的内容都需要被引用(否则,shell 会将空白有效地转换为单个空格,并修剪任何前导或尾随空格)。
当您知道一个变量只能包含一个不包含 shell 元字符的值时,引用是可选的。因此,不带引号的$? 基本上没问题,因为这个变量只能包含一个数字。但是,"$?" 也是正确的,并且建议用于总体一致性和正确性(尽管这是我个人的建议,不是广泛认可的政策)。
不是变量的值基本上遵循相同的规则,尽管您也可以转义任何元字符而不是引用它们。举一个常见的例子,一个带有& 的 URL 将被 shell 解析为后台命令,除非元字符被转义或引用:
$ wget http://example.com/q&uack
[1] wget http://example.com/q
-bash: uack: command not found
(当然,如果 URL 位于未加引号的变量中,也会发生这种情况。)对于静态字符串,单引号最有意义,尽管此处可以使用任何形式的引用或转义。
wget 'http://example.com/q&uack' # Single quotes preferred for a static string
wget "http://example.com/q&uack" # Double quotes work here, too (no $ or ` in the value)
wget http://example.com/q\&uack # Backslash escape
wget http://example.com/q'&'uack # Only the metacharacter really needs quoting
最后一个例子还暗示了另一个有用的概念,我喜欢称之为“跷跷板式报价”。如果需要混合使用单引号和双引号,可以相邻使用。比如下面引用的字符串
'$HOME '
"isn't"
' where `<3'
"' is."
可以背靠背粘贴在一起,在标记化和引号删除后形成一个长字符串。
$ echo '$HOME '"isn't"' where `<3'"' is."
$HOME isn't where `<3' is.
这不是很清楚,但它是一种常见的技术,因此很高兴知道。
顺便说一句,脚本should usually not use ls for anything. 要扩展通配符,只需...使用它。
$ printf '%s\n' $pattern # not ``ls -1 $pattern''
file1.txt
file_other.txt
$ for file in $pattern; do # definitely, definitely not ``for file in $(ls $pattern)''
> printf 'Found file: %s\n' "$file"
> done
Found file: file1.txt
Found file: file_other.txt
(在后一个示例中,循环完全是多余的;printf 特别适用于多个参数。stat 也是。但是循环通配符匹配是一个常见问题,并且经常不正确。)
包含要循环的标记列表或要扩展的通配符的变量不太常见,因此我们有时缩写为“引用所有内容,除非您确切知道自己在做什么”。