How to comment out particular lines in a shell script How to comment out particular lines in a shell script unix unix

How to comment out particular lines in a shell script


You can comment section of a script using a conditional.

For example, the following script:

DEBUG=falseif ${DEBUG}; thenecho 1echo 2echo 3echo 4echo 5fiecho 6echo 7

would output:

67

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.)


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

#!/bin/sh# do valuable stuff heretouch /tmp/a# now comment out all the stuff below up to the EOFecho <<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 /* ... */


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.