Shell Script Put Multiple Line Comment

Posted on in Categories , , , , , , last updated April 1, 2014

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?

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
'

23 comment

  1. you can also put multi-line comments using

    :’
    comment1comment1
    comment2comment2
    comment3comment3
    comment4comment4

  2. 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!

  3. 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!

  4. 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
    
  5. Graham Nicholls Thanks for mentioning about the changes in Ikram Comments. It helped me.

    Thanks Ikram for your post

  6. 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.

  7. #!/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

  8. #!/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"

  9. 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
    #——————————————————————————————————

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

Leave a Comment