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
'

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

23 comment

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

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

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

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

Leave a Comment