Shell Script Put Multiple Line Comments under Bash/KSH

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?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 1m
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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
23 comments… add one
  • Ikram May 14, 2011 @ 15:34

    you can also put multi-line comments using

    :'
    comment1comment1
    comment2comment2
    comment3comment3
    comment4comment4
    '

    • S K Jan 21, 2013 @ 10:56

      Thanks Ikram…..Its working………

    • RAJA G May 12, 2015 @ 10:17

      Thanks Ikram.it is working

    • Johir Sep 8, 2020 @ 10:50

      You will get error if there is a single quote i.e ‘ inside the comment. So, it’s better with

      : <<EOF  
      
      EOF

      N.B: neither need to put colon (:) before << sign nor single quote around first EOF. Since, << is usually used to take input (from user or from a file). So, you can put colon (:) before << to avoid accidental issues.

  • Graham Nicholls Jun 7, 2011 @ 11:01

    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 Apr 23, 2015 @ 8:20

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

  • Ashish Mar 31, 2012 @ 17:59

    Hey, thanks for those descriptions!!!

  • J Durston Apr 15, 2012 @ 14:38

    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 Apr 20, 2012 @ 10:17

    Greatfull lkram.
    Thanks.

  • dianelys Jun 12, 2012 @ 18:18

    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 Aug 20, 2012 @ 20:06

    for bash
    comment="
    comment1comment1
    comment2comment2
    comment3comment3
    comment4comment4
    "

    • Aas Feb 12, 2014 @ 11:36

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

  • Rajasekhar Oct 17, 2012 @ 16:28

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

  • FMC Oct 17, 2012 @ 17:06

    Many thanks for such a good description!

  • Yifang Jul 12, 2013 @ 20:29

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

  • Tux Aug 19, 2013 @ 7:02

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

  • Srini Apr 1, 2014 @ 14:00

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

    Thanks Ikram for your post

  • linuzzer Jun 8, 2014 @ 21:47

    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 Dec 30, 2014 @ 6:23

    Thanks !!

  • QASIM Apr 14, 2015 @ 6:39

    #!/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 Apr 14, 2015 @ 6:40
    #!/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"
    
  • DocSalvager Jun 30, 2016 @ 3:52

    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 Jun 30, 2016 @ 3:55

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.