Thursday, March 13, 2014

Linux - Block of comments in shell script

There is no block comment on shell script however there are few options that you can try out -

Method 1

#!/bin/bash
echo "This is a statement"
: << 'COMMENTS'
echo "The code that you want be commented out goes here"
echo "This echo statement will not be called"
COMMENTS
echo "This statement is outside the comment block"

At the beginning of the block you wish to comment out put -
: << 'COMMENTS'

The '' around the COMMENTS delimiter is important, otherwise things inside the block will be parsed and executed. I have used 'COMMENTS' as it should be a string of characters which is not used in you code, modify it in your code if needed.

And at the end of the block put -
COMMENTS

Method 2

#!/bin/bash
echo "This is a statement"
if [ 1 -eq 0 ]; then
echo "The code that you want be commented out goes here"
echo "This echo statement will not be called"
fi
echo "This statement is outside the comment block"

Added a false condition in the IF block so that it won't get executed.

Method 3

Using vi editor you can easily comment/uncomment from line n to m

Comment

<ESC>
:10,100s/^/#/

Above command says from line 10 to 100 substitute line start (^) with a # sign

Uncomment

<ESC>
:10,100s/^#//

Above command says from line 10 to 100 substitute line start (^) followed by # with noting //