Shell Script Put Multiple Line Comment

by on July 30, 2007 · 14 comments· LAST UPDATED April 1, 2014

in , ,

I would like to use multiline comments under shell script. Under C / C++ I can use the following format

/*
my c code
comment # 2
blah
foo bar
....
*/

How do I put multi line comment under a shell script?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time1m
By default shell script can be commented out prefixing # character, for example:

# my comment goes here
For multiline-comment use the following syntax:
# my comment 1
# my comment 2
# my comment N

However, you can use HERE DOCUMENT feature as follows:

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

Another option as pointed out by Ikram in the comments section below:

#!/bin/bash
foo=bar
: '
This is a test comment
Author foo bar
Released under GNU
'
 
echo "Init..."
# rest of script
 

Please note that the : is shell builtin command. From the bash(1) man page:

: [arguments]
No effect; the command does nothing beyond expanding arguments
and performing any specified redirections. A zero exit code is
returned.

So the syntax is:

 
: '
 your comments here
'
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 14 comments… read them below or add one }

1 Ikram May 14, 2011 at 3:34 pm

you can also put multi-line comments using

:’
comment1comment1
comment2comment2
comment3comment3
comment4comment4

Reply

2 S K January 21, 2013 at 10:56 am

Thanks Ikram…..Its working………

Reply

3 Graham Nicholls June 7, 2011 at 11:01 am

Err, no.
You can’t, at least in bash 4.0.33, which I’m using.

Aha, yes you can, but what is not clear (not your fault, Ikram – just the way the web page displays), is that you need a space between the : and the opening ‘
so:
#!/bin/bash
echo “Hello”
: ‘
comment
comment

echo “Bye”

Works, which is new for me, so thanks!

Reply

4 Ashish March 31, 2012 at 5:59 pm

Hey, thanks for those descriptions!!!

Reply

5 J Durston April 15, 2012 at 2:38 pm

Wow, that’s really useful Ikram.
Where did you find that trick? I think O’reilly are going to have to update thier bash pocket reference book!

Reply

6 Javi April 20, 2012 at 10:17 am

Greatfull lkram.
Thanks.

Reply

7 dianelys June 12, 2012 at 6:18 pm

Hello:
I have this script and I need to comment out the lines 6, 7, 8. I’ve tried using the # but does not work. Can you help me?
Thanks..

for i in `cat cont1`
do
cp $i.DATA EVEC.DATA
./cubemain.x
mv EVEC.0001.cube $i.cube
./trimcube.x -t 0.02 $i.cube > l.cube
mv l.cube $i.cube
#gzip $i
echo $i
done

Reply

8 anon August 20, 2012 at 8:06 pm

for bash
comment=”
comment1comment1
comment2comment2
comment3comment3
comment4comment4

Reply

9 Aas February 12, 2014 at 11:36 am

This wouldn’t do if there is a ” somewhere in the comment.
I’d rather avoid it.

Reply

10 Rajasekhar October 17, 2012 at 4:28 pm

Fantastic tip Ikram..I learnt something new today !!

Reply

11 FMC October 17, 2012 at 5:06 pm

Many thanks for such a good description!

Reply

12 Yifang July 12, 2013 at 8:29 pm

How about the situation when there is same marker ” ‘ within the comment?

Reply

13 Tux August 19, 2013 at 7:02 am

Looks like this works for single line comments too;
: Comment Goes Here

Reply

14 Srini April 1, 2014 at 2:00 pm

Graham Nicholls Thanks for mentioning about the changes in Ikram Comments. It helped me.

Thanks Ikram for your post

Reply

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: