为什么bash的行注释技巧不能与感叹号“!"一起使用? [英] Why does line comment tricks for bash not work with exclamation mark "!"

查看:35
本文介绍了为什么bash的行注释技巧不能与感叹号“!"一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ref 对Bash的内联注释?

我们可以使用这个技巧

echo abc `#put your comment here` \
     def `#another chance for a comment` \
     xyz etc

但是如果我们在注释中带有感叹号,这些功能将无效

but these do not work if we have an exclamation mark in comment

echo 1 `# 2 !3`
<error>
-bash: !3: event not found

如果我们直接键入它,它将不会转换为事件

if we type it directly it will not be translated to an event

# 2 !3
<that is OK>

它表明我们需要另一个#符号来解决该问题.

It seams that we need an other # symbol to workaround it.

echo 1 `# 2 #!3`
<that is OK>
1

还是我们必须将领先的#符号加倍?

or do we have to double the leading # symbol?

echo 1 `# # 2 !3`
<that is OK>
1

推荐答案

(以下解释为 WRONG ,尽管它解释了所有内容.请参见 UPDATE >如下.

(The following explanation turned out to be WRONG though it explained everything. See the UPDATE as follows.)

  1. #!xxx

这可以按预期工作,因为注释中包含.

This works as expected because ! is in the comment.

  1. echo#!xxx

这也可以按预期方式工作,因为也在注释中.

This also works as expected because ! is also in the comment.

  1. echo`true#!xxx`

这也是有效的,因为仍在注释中,尽管它位于`...`上下文中.

This also works because ! is still in the comment, though it's in the `...` context.

  1. echo`#!xxx`

为什么这行不通?
我猜想Bash解释`...`部分时会有一个 bug .在`...`中,Bash始终(错误地)认为 first 单词是COMMAND名称,因此它不认为在评论,因此触发了历史扩展.也就是说, echo`#!xxx` 就像 echo`COMMAND!xxx` .

Why doesn't this work?
I guess there's a little bug when Bash interprets the `...` part. In `...`, Bash always assumes (wrongly) the first WORD is a COMMAND name so it does not think ! is in a comment and so history expansion is triggered. That's to say, echo `# !xxx` is just like echo `COMMAND !xxx`.

  1. echo`##!xxx`

为什么这样做?
#4 中所述,第一个#被解析为COMMAND,所以就像 echo`COMMAND#!xxx` 一样,所以现在在注释中.

Why does this work?
As explained in #4, the first # is parsed as a COMMAND so it's just like echo `COMMAND # !xxx` so now ! is in the comment.

  1. echo`##!xxx`

此双重哈希也不起作用.
#4 #5 中所述,此处 ## 是第一个WORD,它被解析为COMMAND名称,因此也类似于回显"COMMAND!xxx" .

This double hash does not work either.
As explained in #4 and #5, here ## is the first WORD and it's parsed as the COMMAND name so it's also like echo `COMMAND !xxx`.

请注意,在`... 上下文中, bug 仅在第一轮语法 parser 中使用.就是说,即使Bash最初将#解析为COMMAND名称,它也并没有真正将其作为名为#的命令来运行.

Note that, in the `...` context, the bug is only in the first round syntax parser. That's to say, even though Bash initially parses the # as a COMMAND name, it does not really run it as a command which is named #.

上面的解释虽然解释了所有内容,但结果却是错误.请参阅 bug-bash邮件列表中的讨论.

The above explanation turned out to be WRONG though it explained everything. Please see the discussion in bug-bash mailing list.

在这里引用Chet的解释以便于参考:

I'd quote Chet's explanation here for easy reference:

> $ set -H
> $ true `# !xxx`
> bash: !xxx`: event not found

好吧,在记录的开头找不到历史注释字符(#)单词(此处# word `#的一部分),因此该行的其余部分将被处理历史扩展.

Well, the history comment character (#) is not found at the start of a word (here # is part of the word `#), so the rest of the line is processed for history expansion.

$ true `# # !xxx`

历史注释字符位于单词的开头(此处第二个#本身是 word )扩展会跳过其余部分.

The history comment character is found at the start of a word (here the 2nd # itself is a word) and history expansion skips the rest of the line.

Readline历史扩展对shell语法了解甚少;在特别是它不知道反引号.从来没有.

Readline history expansion knows very little about shell syntax; in particular, it doesn't know backquotes. It never has.

这篇关于为什么bash的行注释技巧不能与感叹号“!"一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆