77

Can anyone suggest how to comment particular lines in the shell script other than #?

Suppose I want to comment five lines. Instead of adding # to each line, is there any other way to comment the five lines?

alex
  • 6,818
  • 9
  • 52
  • 103
user2400564
  • 4,619
  • 7
  • 24
  • 27

5 Answers5

36

You can comment section of a script using a conditional.

For example, the following script:

DEBUG=false
if ${DEBUG}; then
echo 1
echo 2
echo 3
echo 4
echo 5
fi
echo 6
echo 7

would output:

6
7

In order to uncomment the section of the code, you simply need to comment the variable:

#DEBUG=false

(Doing so would print the numbers 1 through 7.)

devnull
  • 118,548
  • 33
  • 236
  • 227
26

Yes (although it's a nasty hack). You can use a heredoc thus:

#!/bin/sh

# do valuable stuff here
touch /tmp/a

# now comment out all the stuff below up to the EOF
echo <<EOF
...
...
...
EOF

What's this doing ? A heredoc feeds all the following input up to the terminator (in this case, EOF) into the nominated command. So you can surround the code you wish to comment out with

echo <<EOF
...
EOF

and it'll take all the code contained between the two EOFs and feed them to echo (echo doesn't read from stdin so it all gets thrown away).

Note that with the above you can put anything in the heredoc. It doesn't have to be valid shell code (i.e. it doesn't have to parse properly).

This is very nasty, and I offer it only as a point of interest. You can't do the equivalent of C's /* ... */

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 1
    You probably want to use something other than `echo`, or else send the echo to /dev/null. – ams Aug 21 '13 at 10:10
  • Yes. The command doesn't matter. I do note that on my Cygwin terminal, however, that echo doesn't read stdin. I suspect implementations differ – Brian Agnew Aug 21 '13 at 10:12
  • No, it doesn't *read* anything, but it does *output* a blank line. `true` might be better. – ams Aug 21 '13 at 10:13
  • 1
    Or `:`, which may be more efficient than `true` (although really, neither is going to slow your script down). For the purposes of commenting code, it might be more readable, for the very reason that it doesn't *look* like it's supposed to be doing anything. – chepner Aug 21 '13 at 12:33
10

for single line comment add # at starting of a line
for multiple line comments add ' (single quote) from where you want to start & add ' (again single quote) at the point where you want to end the comment line.

Yash Shah
  • 158
  • 2
  • 13
  • 1
    try to highlight the keywords and be clear with the format it will help to reach out your answer for others – Agilanbu Dec 10 '18 at 07:35
5

You have to rely on '#' but to make the task easier in vi you can perform the following (press escape first):

:10,20 s/^/#

with 10 and 20 being the start and end line numbers of the lines you want to comment out

and to undo when you are complete:

:10,20 s/^#//
S. Morehouse
  • 51
  • 1
  • 5
2

Single Line Comments:

Starts with "#"

For Example:

# This whole line is a comment and will not be executed 

Multi Line Comments:

Starts with "<<commentName" and Ends with "commentName"

For Example:

<<commentName 

Now this whole section is a comment,
until you specify the comment name again
to end the comment section.

commentName
L0tad
  • 574
  • 3
  • 15
Heisenbug
  • 21
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 13 '23 at 04:34