今天我看到一个bash脚本使用冒号来表示评论.使用冒号和哈希标记有什么区别?
: This is a comment.
# This is also a comment.
Run Code Online (Sandbox Code Playgroud)
首先,我知道你不能使用冒号作为结尾评论:
cd somedir : This won't work as a comment.
Run Code Online (Sandbox Code Playgroud)
但上述例子有效的事实让我对如何:评估感到困惑.
:只是一个别名true,并true忽略其参数:
# Does nothing:
true foo bar etc hello
# Does the same:
: foo bar etc hello
Run Code Online (Sandbox Code Playgroud)
它不是注释,不应该用作注释,因为它的所有参数仍然被解析和评估:
: This "comment" actually executes this command: $(touch foo)
ls -l foo
Run Code Online (Sandbox Code Playgroud)
或者像这里一样,StackOverflow的语法高亮显示中间的命令实际上只是文本,即使人类没有:
: The command below won't run:
echo "Hello World"
: because the surrounding "comments" each contain a ' characters
Run Code Online (Sandbox Code Playgroud)