Skip to content
Related Articles

Related Articles

Improve Article

Multi-Line Comment in Shell Script

  • Last Updated : 18 May, 2021

Comments are quite a crucial part of a program or a code-base. It helps in understanding the code, improves code-readability, and also helps in enhancing the structure of the program. We often write single-line comments in our programs as they are quite self-explanatory and require few words to describe the flow of the program or any message about the particular section.  There are rare cases where we want several lines of messages or comment on an entire code block for debugging or any other reasons. We need to have a multiline comment in our program. 

There are two different ways to use multi-line comment in Shell Scripts:

Method 1: Using <<comment:

In Shell or Bash shell, we can comment on multiple lines using << and name of comment.  we start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.

<<comment
 "Code" or "Comments"
comment

The word “comment” can be anything but it should be the same for ending the comment block.



#!/bin/bash
echo "Sample code"
x=4
if [[ $x -le 10 ]];then
    echo "Less than 10"
fi

<<com
echo"This doesn't echo"

echo"Even this doesn't"
com
echo "OK, this is echoing after <<com !"

Output:

Method 2: Using  : ‘ : 

There is also another way to comment in Bash using : ‘ operator.

: '

Code or Comment here
 '

Code:

#!/bin/bash
echo "Sample code"
x=4
if [[ $x -le 10 ]];then
    echo "Less than 10"
fi

: '
echo"This doesn't echo"

echo"Even this doesn't"
 '
echo "OK, this is working with : '"

Output:

So this script also comments multiple lines, we need to be careful here with including a space before the end block ‘ , otherwise it would not work. It also applies to the start comment block : ‘, there is a space in between those characters.

My Personal Notes arrow_drop_up
Recommended Articles
Page :