Dan*_*eck 6

你需要引用它,否则它会被解释为与 shell 可能想要的不同。

几个例子:

您将引用字符串参数foo bar以防止程序由于空格将其解释为两个参数。

# results in two directories, 'foo' and 'bar'.
mkdir foo bar

# results in one directory named 'foo bar'
mkdir "foo bar"

# you could also escape the space to prevent interpretation as argument separator
mkdir foo\ bar
Run Code Online (Sandbox Code Playgroud)

您还引用以防止对您的输入进行一些特殊解释。如果外壳采用$来指示变量名,foo$bar可能会被解释为foo,如果$bar是emoty不确定,甚至产生错误。

bar=qux

# create directory fooqux
mkdir foo$bar

# create directory foo$bar
mkdir 'foo$bar'
Run Code Online (Sandbox Code Playgroud)

作为一种特殊情况,例如在 bash 中,您引用$@(当前命令的参数)以确保它们被传递给单独引用的另一个命令。请参阅此处了解更多信息。

  • 总是很好的阅读:[Quotes](http://mywiki.wooledge.org/Quotes) 来自 Greg 的 Wiki 和 [Quotes and escaping](http://wiki.bash-hackers.org/syntax/quoting) 来自 Bash黑客维基。 (3认同)