Shell script put multiple line comment

by on July 30, 2007 · 10 comments· LAST UPDATED July 30, 2007

in , ,

Q. I’d like to use multiline comments under shell script. Under C / C++ I can use format

/*
my c code
comment # 2
blah
*/

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

A. By default shell script can be commented out prefixing # character for example:
# my comment
For multilinecomment use
# 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"


If you would like to be kept up to date with our posts, you can follow us on Twitter, Facebook, Google+, or even by subscribing to our RSS Feed.


{ 10 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 Rajasekhar October 17, 2012 at 4:28 pm

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

Reply

10 FMC October 17, 2012 at 5:06 pm

Many thanks for such a good description!

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , ,

Previous Faq:

Next Faq: