从“猛砸指南入门”:如何在linux shell中使用反引号?
3.4.5. Command substitution
Command substitution allows the output of a command to replace the command itself. Command substitution
occurs when a command is enclosed like this:
$(command)
or like this using backticks:
\`command`
Bash performs the expansion by executing COMMAND and replacing the command substitution with the
standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but
they may be removed during word splitting.
franky ~> echo `date`
Thu Feb 6 10:06:20 CET 2003
When the old−style backquoted form of substitution is used, backslash retains its literal meaning except when
followed by "$", "`", or "\".
The first backticks not preceded by a backslash terminates the command substitution.
When using the "$(COMMAND)" form, all characters between the parentheses make up the
command; none are treated specially.
在这段话,有一句话我不明白。
The first backticks not preceded by a backslash terminates the command substitution.
你能否提供例子来更详细地解释它? 非常感谢!
最好使用'$()'而不是反引号。但最后一句话意味着如果反引号即“\\''之前有一个反斜杠,那么它将被看作是文字反引号而不是命令的终结符。 – 123
@ User112638726感谢您的评论!但是,你能告诉我如何使用它(即:\\'),我应该在什么时候在linux中使用它,对于不终止命令有什么意义?对不起,我的英语很差,我不知道你能理解我。 – Guo
老实说,应该有一个使用反引号而不是'$()'的理由。你会逃避反拨的原因通常是将它们嵌套在对方内,但是你实际上不想嵌套反引号。 – 123