2015-07-28 67 views
1

从“猛砸指南入门”:如何在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. 

你能否提供例子来更详细地解释它? 非常感谢!

+3

最好使用'$()'而不是反引号。但最后一句话意味着如果反引号即“\\''之前有一个反斜杠,那么它将被看作是文字反引号而不是命令的终结符。 – 123

+1

@ User112638726感谢您的评论!但是,你能告诉我如何使用它(即:\\'),我应该在什么时候在linux中使用它,对于不终止命令有什么意义?对不起,我的英语很差,我不知道你能理解我。 – Guo

+1

老实说,应该有一个使用反引号而不是'$()'的理由。你会逃避反拨的原因通常是将它们嵌套在对方内,但是你实际上不想嵌套反引号。 – 123

回答

1

作者的意思是第一次没有逃脱的倒勾。

假人例如:

echo `command \` arg` 

第一命令和Arg之间的反引号被转义与反斜杠,所以替代由最后的反引号关闭。

+1

您需要在应答时避开反引号,或者将它们置于代码块中。 – 123

+0

@ User112638726谢谢。现在已经修复了。 –

+0

@ tsv.dimitrov虽然我不太明白,但谢谢你的帮助。 – Guo