是否有一种简单的方法来注释掉shell脚本中的代码块?


当前回答

如果你能避开单引号:

__='
blah blah comment.
'

其他回答

如果你能避开单引号:

__='
blah blah comment.
'

使用:'打开'和'关闭。

例如:

: '
This is a
very neat comment
in bash
'

这是维加斯的例子

你可以使用:

if [ 1 -eq 0 ]; then
  echo "The code that you want commented out goes here."
  echo "This echo statement will not be called."
fi

sunny256给出的已接受的答案中的here-doc技巧的一个变体是使用Perl关键字进行注释。如果您的注释实际上是某种文档,那么您可以开始在注释块中使用Perl语法,这允许您将其打印出来,将其转换为手册页,等等。

就shell而言,你只需要将'END'替换为'=cut'。

echo "before comment"
: <<'=cut'
=pod

=head1 NAME
   podtest.sh - Example shell script with embedded POD documentation

etc.

=cut
echo "after comment"

(参见“在shell脚本中嵌入文档”)

在一个:

去你想评论的第一行 shift-V(进入可视模式),向上向下突出块中的行 执行以下选择:s/^/#/ 命令如下所示: : <、>年代/ ^ / # 回车

e.g.

shift-V
jjj
:s/^/#
<enter>