Is there a simple way to comment out a block of code in a shell script?
In bash:
#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment
The '
and '
around the END
delimiter are important, otherwise things inside the block like for example $(command)
will be parsed and executed.
EOF
is a classic example (and so is !
, an exclamation mark on its own), but you could use SNURFLE_BURGERS
or classical_end_marker
or any other word that doesn't appear on a line on its own in the commented-out material. I'd be leary of experimenting with spaces etc, but the word might well work with them too. –
Tented echo before comment : <<EOF
and automatically inserts the block with a new line and and EOF
at the end if the script you are working in is already saved as .sh . –
Elga :
) that doesn't read its input and always exits with a successful value, and sending the "comment" as input. Not much to it. –
Herbal :
(that is just <<'EOF' ... ... .. EOF
and it seems to work as well. I don't understand why. –
Bovid Use : '
to open and '
to close.
For example:
: '
This is a
very neat comment
in bash
'
This is from Vegas's example found here
'
in there. –
Flop There is no block comment on shell script.
Using vi
(yes, vi
) you can easily comment from line n to m
<ESC>
:10,100s/^/#/
(that reads, from line 10 to 100 substitute line start (^) with a # sign.)
and un comment with
<ESC>
:10,100s/^#//
(that reads, from line 10 to 100 substitute line start (^) followed by # with noting //.)
vi
is almost universal anywhere where there is /bin/sh
.
|noh
to the end. The pipe separates additional commands and noh
is for nohighlight. Search term highlighting will automatically resume the next time you search for something. Example: :10,100s/^/#/g|noh
–
Conative <SHIFT>+G 10 <ENTER>
then 0
or by any other way to navigate). Then use <CTRL>+V
to enter visual block mode and highlight the beginning of all lines you want to comment (in this example 90 J
). Then press SHIFT+I
to insert before the highlighted block. Enter the comment sign (e.g. #
) and press <ESC>
to finish your prefixing. This explanation sounds super long, but in my experience it is much faster in practice. –
Impeccant You can use:
if [ 1 -eq 0 ]; then
echo "The code that you want commented out goes here."
echo "This echo statement will not be called."
fi
if [ ];
also works. –
Bottoms if false;
. https://mcmap.net/q/82456/-is-there-a-quot-goto-quot-statement-in-bash –
Josey The following should work for sh
,bash
, ksh
and zsh
.
The blocks of code to be commented can be put inside BEGINCOMMENT
and ENDCOMMENT
:
[ -z $BASH ] || shopt -s expand_aliases
alias BEGINCOMMENT="if [ ]; then"
alias ENDCOMMENT="fi"
BEGINCOMMENT
echo "This line appears in a commented block"
echo "And this one too!"
ENDCOMMENT
echo "This is outside the commented block"
Executing the above code would result in:
This is outside the commented block
In order to uncomment the code blocks thus commented, say
alias BEGINCOMMENT="if : ; then"
instead of
alias BEGINCOMMENT="if [ ]; then"
in the example above.
if you can dodge the single quotes:
__='
blah blah comment.
'
In Vim:
- go to first line of block you want to comment
shift-V
(enter visual mode), up down highlight lines in block- execute the following on selection
:s/^/#/
the command will look like this:
:'<,'>s/^/#
hit enter
e.g.
shift-V
jjj
:s/^/#
<enter>
:s/^#/
–
Rook You could use Vi/Vim's Visual Block mode which is designed for stuff like this:
Ctrl-V
Highlight first element in rows you want commented
Shift-i
#
esc
Uncomment would be:
Ctrl-V
Highlight #'s
d
l
This is vi's interactive way of doing this sort of thing rather than counting or reading line numbers.
Lastly, in Gvim you use ctrl-q to get into Visual Block mode rather than ctrl-v (because that's the shortcut for paste).
In all honesty, why so much overengineering...
I consider it really a bad practice to write active code for generating passive code.
My solution: most editors have block select mode. Just use it to add # to all lines you want to comment out. What's the big deal...
Notepad example:
To create: Alt - mousedrag down, press #.
To delete: Alt-mousedrag down, shift-right arrow, delete.
You can put the code to comment inside a function. A good thing about this is you can "uncomment" by calling the function just after the definition.
Unless you plan to "uncomment" by calling the function, the text inside the function does not have to be syntactically correct.
ignored() {
echo this is comment
echo another line of comment
}
Many GUI editors will allow you to select a block of text, and press "{" to automatically put braces around the selected block of code.
A variation on the here-doc trick in the accepted answer by sunny256 is to use the Perl keywords for comments. If your comments are actually some sort of documentation, you can then start using the Perl syntax inside the commented block, which allows you to print it out nicely formatted, convert it to a man-page, etc.
As far as the shell is concerned, you only need to replace 'END'
with '=cut'
.
echo "before comment"
: <<'=cut'
=pod
=head1 NAME
podtest.sh - Example shell script with embedded POD documentation
etc.
=cut
echo "after comment"
(Found on "Embedding documentation in shell script")
I like a single line open and close:
if [ ]; then ##
...
...
fi; ##
The '##' helps me easily find the start and end to the block comment. I can stick a number after the '##' if I've got a bunch of them. To turn off the comment, I just stick a '1' in the '[ ]'. I also avoid some issues I've had with single-quotes in the commented block.
Let's combine the best of all of these ideas and suggestions.
alias _CommentBegin_=": <<'_CommentEnd_'"
as has been said, the single quote is very important, in that without them $(commandName) and ${varName} would get evaluated.
You would use it as:
_CommentBegin_
echo "bash code"
or
none code can be in here
_CommentEnd_
The alias makes the usage more obvious and better looking.
Another mode is: If your editor HAS NO BLOCK comment option,
- Open a second instance of the editor (for example File=>New File...)
- From THE PREVIOUS file you are working on, select ONLY THE PART YOU WANT COMMENT
- Copy and paste it in the window of the new temporary file...
- Open the Edit menu, select REPLACE and input as string to be replaced '\n'
- input as replace string: '\n#'
- press the button 'replace ALL'
DONE
it WORKS with ANY editor
This can be done in a shorter syntax in sh, bash and ksh
<< COMMENT
They worked in the cold, so we could be warm,
some died in the evening, some died at dawn.
They breathed in the coal dust, so kids would be fed,
Gone are those miners, all of them dead.
COMMENT
© 2022 - 2024 — McMap. All rights reserved.