考虑
echo \ # this is a comment
foo
这给出:
$ sh foo.sh
# this is a comment
foo.sh: line 2: foo: command not found
在网上搜索后,我在姐妹网站Stack Overflow上找到了DigitalRoss的解决方案。所以一个人可以做
echo `: this is a comment` \
foo
或者
echo $(: this is a comment) \
foo
但是,DigitalRoss没有解释这些解决方案为何起作用。我希望得到一个解释。他回答说:
以前有一个shell
goto
命令,它会分支到:
此处指定的标签。该goto
走了,但你仍然可以使用的: whatever
语法...:
是一种解析的评论了。
但我想要更多细节和背景信息,包括可移植性的讨论。
当然,如果有人有其他解决方案,那也很好。
另请参见前面的问题如何在Shell脚本中注释多行命令?。
从下面的讨论中带回家的消息。该`: this is a comment`
只是一个命令替换。的输出: this is a comment
为空,它将代替`: this is a comment`
。
更好的选择如下:
echo `# this is a comment` \
foo