≡ 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
foo bar

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

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:

echo "Say Something"
    your comment 1
    comment 2
echo "Do something else"

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

: '
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

So the syntax is:

: '
 your comments here
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 23 comments… add one }
  • Ikram May 14, 2011, 3:34 pm

    you can also put multi-line comments using


  • 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 ‘
    echo “Hello”
    : ‘

    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.

  • dianelys June 12, 2012, 6:18 pm

    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?

    for i in `cat cont1`
    cp $i.DATA EVEC.DATA
    mv EVEC.0001.cube $i.cube
    ./trimcube.x -t 0.02 $i.cube > l.cube
    mv l.cube $i.cube
    #gzip $i
    echo $i
  • anon August 20, 2012, 8:06 pm

    for bash

    • 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

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

    echo after comment

  • QASIM April 14, 2015, 6:40 am

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

    echo "after comment"

  • DocSalvager June 30, 2016, 3:52 am

    The colon(:) is a command that does nothing, called a “NOP”(no operation). Think of it as an ‘echo’ command that doesn’t print anything. It is rarely used. It is NOT a comment. What all of these examples are doing is creating a string literal using either single- or double-quotes.

    Unlike C, bash only has the hash(#) for comments. Everything following a # on a line is ignored by the interpreter. Thus, multiline comments require hash characters at the begining of each line. They don’t have to be at the left edge. They can be indented with spaces and/or tab characters as in…

    # comment

    # multiline comment
    # multiline comment
    # multiline comment

    # Indented multiline comment
    # Indented multiline comment
    # Indented multiline comment

    ### multiple hashes work fine too

    # boxes and lines must start with a hash

  • DocSalvager June 30, 2016, 3:55 am

    This system’s editor removed the leading spaces from the indented lines on my last comment and does not allow me to edit it.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , ,