问题
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
回答7:
Simple solution, not much smart:
Temporarily block a part of a script:
if false; then
while you respect syntax a bit, please
do write here (almost) whatever you want.
but when you are
done # write
fi
A bit sophisticated version:
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
回答8:
I tried the chosen answer, but found when I ran a shell script having it, the whole thing was getting printed to screen (similar to how jupyter notebooks print out everything in '''xx''' quotes) and there was an error message at end. It wasn't doing anything, but: scary. Then I realised while editing it that single-quotes can span multiple lines. So.. lets just assign the block to a variable.
x='
echo "these lines will all become comments."
echo "just make sure you don_t use single-quotes!"
ls -l
date
'
回答9:
# I like laziness and simplicity. I'd use # with a funny workaround:
1 PRESS: ]find ctrl+F or cmd+F or whatever[ to trigger the find functionality
2 use a regex in the find field like: (^.+)
3 replace with: # $1 or if you prefer #$1
# Note: you may not have the three steps in your editor. In that case use an online regex tool (cannot suggest one here for policy reasons):
- Select, copy the text wherever you are and paste it in the online regex tool
- Use
(^.+)as regex and#$1or#\1as substitution patterns - Select, copy the text and paste it back where you started
# Enjoy your hashes!
来源:https://stackoverflow.com/questions/43158140/way-to-create-multiline-comments-in-bash