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?
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?
Use
: '
to open and'
to close.For example:
what's your opinion on this one?
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:
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):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.
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:
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.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.
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:
Multiline comment in bash