Commenting out a set of lines in a shell script

UnixShellCommentsEditing

Unix Problem Overview


I was wondering if there is a way to comment out a set of lines in a shell script. How could I do that? We can use /* */ in other programming languages. This is most useful when I am converting/using/modifying another script and I want to keep the original lines instead of deleting.

It seems a cumbersome job to find and prefix # for all the lines which are not used.

Lets say there are 100 lines in the script in consequent lines which are not to used. I want to comment them all out in one go. Is that possible?

Unix Solutions


Solution 1 - Unix

The most versatile and safe method is putting the comment into a void quoted here-document, like this:

<<"COMMENT"
    This long comment text includes ${parameter:=expansion}
    `command substitution` and $((arithmetic++ + --expansion)).
COMMENT

Quoting the COMMENT delimiter above is necessary to prevent parameter expansion, command substitution and arithmetic expansion, which would happen otherwise, as Bash manual states and POSIX shell standard specifies.

In the case above, not quoting COMMENT would result in variable parameter being assigned text expansion, if it was empty or unset, executing command command substitution, incrementing variable arithmetic and decrementing variable expansion.

Comparing other solutions to this:

Using if false; then comment text fi requires the comment text to be syntactically correct Bash code whereas natural comments are often not, if only for possible unbalanced apostrophes. The same goes for : || { comment text } construct.

Putting comments into a single-quoted void command argument, as in :'comment text', has the drawback of inability to include apostrophes. Double-quoted arguments, as in :"comment text", are still subject to parameter expansion, command substitution and arithmetic expansion, the same as unquoted here-document contents and can lead to the side-effects described above.

Using scripts and editor facilities to automatically prefix each line in a block with '#' has some merit, but doesn't exactly answer the question.

Solution 2 - Unix

if false
then

...code...

fi

false always returns false so this will always skip the code.

Solution 3 - Unix

You can also put multi-line comments using:

: '
comment1comment1
comment2comment2
comment3comment3
comment4comment4
'

As per the Bash Reference for Bourne Shell builtins >: (a colon) > > : [arguments] > > Do nothing beyond expanding arguments and performing redirections. The return status is zero.

Thanks to Ikram for pointing this out in the post Shell script put multiple line comment

Solution 4 - Unix

You can use a 'here' document with no command to send it to.

#!/bin/bash
echo "Say Something"
<<COMMENT1
    your comment 1
    comment 2
    blah
COMMENT1
echo "Do something else"

Wikipedia Reference

Solution 5 - Unix

Text editors have an amazing feature called search and replace. You don't say what editor you use, but since shell scripts tend to be *nix, and I use VI, here's the command to comment lines 20 to 50 of some shell script:

:20,50s/^/#/

Solution 6 - Unix

: || {
your code here
your code here
your code here
your code here
}

Solution 7 - Unix

What if you just wrap your code into function?

So this:

cd ~/documents
mkdir test
echo "useless script" > about.txt

Becomes this:

CommentedOutBlock() {
  cd ~/documents
  mkdir test
  echo "useless script" > about.txt
}

Solution 8 - Unix

As per this site:

#!/bin/bash
foo=bar
: '
This is a test comment
Author foo bar
Released under GNU 
'

echo "Init..."
# rest of script

Solution 9 - Unix

Depending of the editor that you're using there are some shortcuts to comment a block of lines.

Another workaround would be to put your code in an "if (0)" conditional block ;)

Solution 10 - Unix

This Perl one-liner comments out lines 1 to 3 of the file orig.sh inclusive (where the first line is numbered 0), and writes the commented version to cmt.sh.

perl -n -e '$s=1;$e=3; $_="#$_" if $i>=$s&&$i<=$e;print;$i++' orig.sh > cmt.sh

Obviously you can change the boundary numbers as required.

If you want to edit the file in place, it's even shorter:

perl -in -e '$s=1;$e=3; $_="#$_" if $i>=$s&&$i<=$e;print;$i++' orig.sh

###Demo### $ cat orig.sh a b c d e f

$ perl -n -e '$s=1;$e=3; $_="#$_" if $i>=$s&&$i<=$e;print;$i++' orig.sh > cmt.sh

$ cat cmt.sh 
a
#b
#c
#d
e
f

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijayView Question on Stackoverflow
Solution 1 - UnixspbnickView Answer on Stackoverflow
Solution 2 - UnixArteliusView Answer on Stackoverflow
Solution 3 - Unixasev69View Answer on Stackoverflow
Solution 4 - UnixBuggabillView Answer on Stackoverflow
Solution 5 - UnixkdgregoryView Answer on Stackoverflow
Solution 6 - Unixjj_5698View Answer on Stackoverflow
Solution 7 - UnixBuksyView Answer on Stackoverflow
Solution 8 - UnixgjoisView Answer on Stackoverflow
Solution 9 - UnixCarlos TasadaView Answer on Stackoverflow
Solution 10 - Unixire_and_cursesView Answer on Stackoverflow