我最近开始研究shell脚本,我希望能够在shell脚本中注释出一组行。我的意思是,就像在C/Java的情况下:

/* comment1
   comment2 
   comment3
*/`

我怎么能那样做呢?


当前回答

简单的解决方案,不太聪明:

暂时阻塞脚本的一部分:

if false; then
    while you respect syntax a bit, please
    do write here (almost) whatever you want.
    but when you are
    done # write
fi

稍微复杂一点的版本:

time_of_debug=false # Let's set this variable at the beginning of a script

if $time_of_debug; then # in a middle of the script  
    echo I keep this code aside until there is the time of debug!
fi

其他回答

直白地说 注释掉 一段代码 我做

: | | { 块 的代码 }

bash中的多行注释

: <<'END_COMMENT'
This is a heredoc (<<) redirected to a NOP command (:).
The single quotes around END_COMMENT are important,
because it disables variable resolving and command resolving
within these lines.  Without the single-quotes around END_COMMENT,
the following two $() `` commands would get executed:
$(gibberish command)
`rm -fr mydir`
comment1
comment2 
comment3
END_COMMENT

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

例如:

: '
This is a
very neat comment
in bash
'

你对此有什么看法?

function giveitauniquename()
{
  so this is a comment
  echo "there's no need to further escape apostrophes/etc if you are commenting your code this way"
  the drawback is it will be stored in memory as a function as long as your script runs unless you explicitly unset it
  only valid-ish bash allowed inside for instance these would not work without the "pound" signs:
  1, for #((
  2, this #wouldn't work either
  function giveitadifferentuniquename()
  {
    echo nestable
  }
}

在阅读了其他答案后,我想出了以下答案,恕我直言,这是一个评论。特别适合脚本内使用信息:

<< ////

Usage:
This script launches a spaceship to the moon. It's doing so by 
leveraging the power of the Fifth Element, AKA Leeloo.
Will only work if you're Bruce Willis or a relative of Milla Jovovich.

////

作为一名程序员,斜杠的序列会立即在我的大脑中注册为注释(尽管斜杠通常用于行注释)。

当然,“////”只是一个字符串;前缀和后缀中的斜杠个数必须相等。