Way to create multiline comments in Bash?

2019-01-29 21:13发布

问题:

I have recently started studying shell script and I'd like to be able to comment out a set of lines in a shell script. I mean like it is in case of C/Java :

/* comment1
   comment2 
   comment3
*/`

How could I do that?

回答1:

Use : ' to open and ' to close.

For example:

: '
This is a
very neat comment
in bash
'


回答2:

Multiline comment in 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


回答3:

Bash does not provide a builtin but there are hacks using existing bash syntax. The simplest is to use a HEREDOC, but make it obvious what you are doing, and use the same HEREDOC marker everywhere:

<< --MULTILINE-COMMENT--
line 1
line 2

line 3
line 4
--MULTILINE-COMMENT--

Some posts mention that the HEREDOC marker must be quoted in order to avoid some shell parsing side-effects. I have only found this to be necessary if you use backquotes in your comment. Even with set -o verbose and $variables mentioned in the comment, quoting the marker is not necessary. YMMV.

If you use the : ' approach mentioned in another answer, then document what it is via a meta-comment, use the same meta-comment everywhere, and remember to double any occurrences of ' within the comment (a syntax coloring editor will make it obvious):

: 'Multiline comment:
line 1
line 2 we''re going to try this eventually
line 3
'

Both are hacks so they could break scripts in the future.

There are surely other techniques, but there doesn't seem to be a "conventional" way to do it.



回答4:

After reading the other answers here I came up with the below, which IMHO makes it really clear it's a comment. Especially suitable for in-script usage info:

<< ////

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.

////

As a programmer, the sequence of slashes immediately registers in my brain as a comment (even though slashes are normally used for line comments).

Of course, "////" is just a string; the number of slashes in the prefix and the suffix must be equal.



回答5:

what's your opinion on this one?

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
  }
}


回答6:

Here's how I do multiline comments in bash.

This mechanism has two advantages that I appreciate. One is that comments can be nested. The other is that blocks can be enabled by simply commenting out the initiating line.

#!/bin/bash
# : <<'####.block.A'
echo "foo {" 1>&2
fn data1
echo "foo }" 1>&2
: <<'####.block.B'
fn data2 || exit
exit 1
####.block.B
echo "can't happen" 1>&2
####.block.A

In the example above the "B" block is commented out, but the parts of the "A" block that are not the "B" block are not commented out.

Running that example will produce this output:

foo {
./example: line 5: fn: command not found
foo }
can't happen