Cheran
Cheran

Reputation: 95

Shell script multi-line comment

I am having a large shell script file. At times while doing modification I want to comment out part of it. But commenting line as shown in the below example is giving me error.

Script:

#!/bin/bash
<<COMMENT1
read build_label
read build_branch_tag
build_date_tag=$(echo $build_label | sed "s/$build_branch_tag//g")
echo $build_path
COMMENT1
echo "HELLO WORLD"

Error Message:

sed: first RE may not be empty

I just want to understand what's wrong with the above script and why comment section is not working properly.

Upvotes: 3

Views: 9520

Answers (5)

Richard Urban
Richard Urban

Reputation: 399

Using here docs to comment code is safe and elegant like this:

: <<'EOT'
Example usage of the null command ':' and the here-document syntax for a 
multi-line comment.  If the delimiter word ('EOT' here) is quoted, the 
here-document will not be expanded in any way.  This is important, as 
an unquoted delimiter will result in problems with unintended potential 
expansions.  All of this here-doc text is redirected to the standard input 
of :, which does nothing but return true.  
EOT

Upvotes: 0

user8175891
user8175891

Reputation:

You can turn parameter substitution off inside a here document like this:

<<"Endofmessage"

or

<<'Endofmessage'

or

<<\Endofmessage

Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command. The format of here-documents is:

<<[-]word here-document delimiter No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \ is ignored, and \ must be used to quote the characters \, $, and `. If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

And maybe something that you may also like: I prefer to do multiline comments in my bash script with the nodepad++ shortcut ctrl+Q (toggle comment).

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

if this is not a syntax error (open string, ...)

#!/bin/bash

if false;then
read build_label
read build_branch_tag
build_date_tag=$(echo $build_label | sed "s/$build_branch_tag//g")
echo $build_path
fi

echo "HELLO WORLD"

if sysntax error or equivalent (unfound place like in search of error by descativate part of failing code)

#!/bin/bash

#read build_label
#read build_branch_tag
#build_date_tag=$(echo $build_label | sed "s/$build_branch_tag//g")
#echo $build_path

echo "HELLO WORLD"

for this you can use: - editor if find/replace with regex is available like vi(m) - a sed (sed '14,45 s/^/#/' YourFile > YourFile.Debug where 14 and 45 are first and last lines to comment)

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158210

First, using here docs to comment code is really dirty! Use the # instead. If you want to comment multiple lines, use your editor. In vim (commenting lines from 10 to 15 for example):

:10,15s/^/#

However, to solve your current problem you need to enclose the starting here-doc delimiter in single quotes, like this:

<<'COMMENT'
...
COMMENT

Using single quotes you tell bash that it should not attempt to expand variables or expression inside the here doc body.

Upvotes: 9

DarkDust
DarkDust

Reputation: 92384

Traditional UNIX shell doesn't have multiline comment support. What you're doing here is using a so-called "HERE document" without using its value, a common hack to get multiline comment like behaviour.

However, patterns inside the the HERE document are still evaluated, which means that your $(…) is executed. But since build_branch_tag has not been defined before, it will evaluate to an empty string, and the shell will thus execute sed s///g.

You can use a different hack:

: '
Bla bla, no $expansion is taking place here.
'

What this is doing: the : is a no-op command, it simply does nothing. And you're passing it an argument which is a string '…'. Inside the single quotes, no expansion/evaluation is taking place. Beware of ' inside the "commented out" region, though.

Upvotes: 2

Related Questions