How to comment out particular lines in a shell script

user2400564 picture user2400564 · Aug 21, 2013 · Viewed 225.6k times · Source

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?

Answer

devnull picture devnull · Aug 21, 2013

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