Shell Script Put Multiple Line Comments under Bash/KSH

last updated in Categories , , , , , ,

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? Can you tell method to set multiline shell script comments?

By default shell script can be commented out prefixing # character, for example:
# my comment goes here

ADVERTISEMENTS


Bash Shell Script Put Multiple Line Comment Syntax

For multiline comment use the following syntax:

#!/usr/bin/env bash
# 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"

This type of redirection tells the shell to read input from the current source (HERE) until a line containg only word (HERE) is seen. HERE word is not subjected to variable name, parameter expansion, arithmetic expansion, pathname expansion, or command substitution. All of the lines read up to that point are then used as the standard input for a command. Files are processed in this manner are commonly called here documents. If you do not want variable name, parameter expansion, arithmetic expansion, pathname expansion, or command substitution quote HERE (COMMENTS) in a single quote:

<<'COMMENTS'
text1
text2
testN
$varName
COMMENTS

Bash comment block example

It is important that you put EOF in a single quote ('EOF') to avoid command execution and phrasing. Here is block comments in a shell script example:

#!/usr/bin/env bash
echo "*** Before comment block ***"
: <<'EOF'
CODE block starts
CODE block ends here
EOF
echo "*** After comments block ***"
Shell Script Put Multiple Comments under Linux and Unix bash or ksh
Writing multi-line comments in Bash scripts

Multiline shell script comments

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
'

Null command used in your shell script to put multiple comments. The ':' has no effect. In other words, the command does nothing and it always exit with succeed code.

Conclusion

It would be best if you documented your script and writing comments considered as a best practice. Other sysadmins and developers might use comments to understand your scripts. As explained eariler, anything is written after # and till the end of the line is nothing but comments.

You learned about multiline shell script comments possibilities. However, shells do not offer a multiline comment syntax. Hence, we learned and used various techniques such as here-documents to make multiline "comments" under Linux or Unix-like systems.

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

  1. As far as I know, you cannot.
    a trick can be using a “:” statement, like here:

    my code
    my code
    : '
    everythig here isnot executed
    '
    my code
    my code

Continue the discussion www.nixcraft.com

Participants

Historical Comment Archive

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!

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

  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.

    Still, have a question? Get help on our forum!