≡ Menu

Shell Script Put Multiple Line Comment

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
'
 
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 20 comments… add one }

  • Ikram May 14, 2011, 3:34 pm

    you can also put multi-line comments using

    :’
    comment1comment1
    comment2comment2
    comment3comment3
    comment4comment4

    • S K January 21, 2013, 10:56 am

      Thanks Ikram…..Its working………

    • RAJA G May 12, 2015, 10:17 am

      Thanks Ikram.it is working

  • Graham Nicholls June 7, 2011, 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!

    • S D April 23, 2015, 8:20 am

      Thanks Nicholls for finding out the space between the : and the opening ‘
      :) thanks to Ikram as well!

  • Ashish March 31, 2012, 5:59 pm

    Hey, thanks for those descriptions!!!

  • J Durston April 15, 2012, 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!

  • Javi April 20, 2012, 10:17 am

    Greatfull lkram.
    Thanks.

  • dianelys June 12, 2012, 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
    
  • anon August 20, 2012, 8:06 pm

    for bash
    comment=”
    comment1comment1
    comment2comment2
    comment3comment3
    comment4comment4

    • Aas February 12, 2014, 11:36 am

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

  • Rajasekhar October 17, 2012, 4:28 pm

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

  • FMC October 17, 2012, 5:06 pm

    Many thanks for such a good description!

  • Yifang July 12, 2013, 8:29 pm

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

  • Tux August 19, 2013, 7:02 am

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

  • Srini April 1, 2014, 2:00 pm

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

    Thanks Ikram for your post

  • linuzzer June 8, 2014, 9:47 pm

    The “comments” with “:” can sometimes execute code that is within the comment, if it’s not written carefully to avoid it from happening. Therefore it may be more adequate to use it for textual comments rather than commenting out sections of code.

    See here.

  • Ram December 30, 2014, 6:23 am

    Thanks !!

  • QASIM April 14, 2015, 6:39 am

    #!/bin/bash
    echo before comment
    : <<'END'
    enter anything here, this is comment box and this is best i have seen
    :'
    ' does not work for all cases

    END
    echo after comment

  • QASIM April 14, 2015, 6:40 am

    #!/bin/bash
    #corrected
    echo “before comment”
    : <<'END'
    enter anything here, this is comment box and this is best i have seen
    :'
    ' does not work for all cases

    END
    echo "after comment"

Leave a Comment