Wie*_*err 100 bash shell-script
最近,我偶然发现了一种我以前从未见过的多行注释类型——这是一个脚本示例:
echo a
#
: aaa
: ddd
#
echo b
Run Code Online (Sandbox Code Playgroud)
这似乎有效,甚至vim语法突出显示它。这种评论风格是什么,我如何找到有关它的更多信息?
jw0*_*013 145
那不是多行注释。 #是单行注释。
:(colon)根本不是注释,而是一个 shell 内置命令,它基本上是一个NOP,一个空操作,除了返回 true 之外什么都不做,就像true(因此设置$?为 0 作为副作用)。然而,由于它是一个命令,它可以接受参数,并且由于它忽略了它的参数,在大多数情况下,它表面上就像一个注释。这种混乱的主要问题是争论仍在扩大,导致许多意想不到的后果。参数仍然受语法错误的影响,仍然会执行重定向,因此: > file将 truncate file,并且: $(dangerous command)替换仍然会运行。
在 shell 脚本中插入注释的最不令人惊讶的完全安全的方法是使用#. 即使对于多行注释,也要坚持这一点。 切勿尝试 (ab):用于评论。shell 中没有专门的多行注释机制,类似于类语言中的斜线-星/* */形式C。
为了完整起见,但不是因为它是推荐的做法,我会提到可以使用here-documents进行多行“注释”:
: <<'end_long_comment'
This is an abuse of the null command ':' and the here-document syntax
to achieve a "multi-line comment". According to the POSIX spec linked
above, if any character in the delimiter word ("end_long_comment" in
this case) above is quoted, the here-document will not be expanded in
any way. This is **critical**, as failing to quote the "end_long_comment"
will result in the problems with unintended expansions described above.
All of this text in this here-doc goes to the standard input of :, which
does nothing with it, hence the effect is like a comment. There is very
little point to doing this besides throwing people off. Just use '#'.
end_long_comment
Run Code Online (Sandbox Code Playgroud)
Ign*_*ams 29
这不是任何评论风格。在:内置的命令绝对没有; 在这里评论被滥用了。
$ help :
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.
Run Code Online (Sandbox Code Playgroud)
小智 26
在早期的 shell 中,冒号是创建注释的唯一方式。
但是,这不是真正的注释,因为该行的解析方式与解析任何其他命令的方式完全相同,并且可能会产生副作用。例如:
: ${a:=x} # assigns the value 'x' to the variable, 'a'
: $(command) # executes 'command'
Run Code Online (Sandbox Code Playgroud)
(有时冒号仅用于调用这些副作用,但它不会被用作注释。)
有时使用冒号来注释掉脚本的一部分是很方便的:
: '
while [ "$n" -ne "$x" ]
do
: whatever
done
'
Run Code Online (Sandbox Code Playgroud)
这比在每一行前面加上 节省了大量时间#,特别是如果注释掉只是暂时的。