Bash Comments


How to use single and multiple line comments in BASH

Using comments in any script or code is very important to make the script more readable. Comments work as a documentation for the script. The reader can easily understand each step of the script if it is properly commented by the author. Comments are ignored when the script executes. The single line can be commented very easily in the bash script. But there are multiple ways to comment multiple lines in the bash script. How you can use single and multiple lines comments in bash scripts is shown in this tutorial.

Single line comment:

You can explain the function of each line of the script by adding single line comment on the above or side of the line. ‘#’ symbol is used to comment single line in the bash script. The following example shows the use of single line comment.

Example-1: Single line comment

#Print a simple text
echo "Working with bash comments"
#Add 10 with 20 and store the value in n
#Print the value of n
echo $n


Three single line comments are used in the above script and those lines are ignored in the output.

Multiple line comments:

There is no direct option to comment multiple lines in the bash script. You can use other features of bash to comment multiple lines in a script. One option is using ‘here document’ and another option is using ‘:’. The uses of both options are shown in the following examples.

Example-2: Multiple line Comment using here document

Here, LongComment is used as here document delimiter to add the multiline comment.

This script is used to
Calculate the cube of
a number with value 5

#Set the value of n
#Calculate 5 to the power 3
#Print the area
echo $result


All comments are ignored in the output.

Example-3: Multiline comment using ‘:’ command

Write the multiline comment using single quote after ‘:’.

#Initialize the variable n with a number
The following script will check the number is
Even or odd by dividing the number by 2 and checking the remainder value

if (( $n % 2 == 0 ))
     echo "The number is even"

     echo "The number is odd"


All comments are ignored in the output.

Hope, this tutorial will help you to learn and apply single and multiple line comments in your bash script.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Find Length of String in Bash

The total number of characters of any string data indicates the length of the string. When we work with string data then...

tput, printf and shell expansions: how to create awesome outputs with bash scripts?

1. Why are good outputs so important in bash scripts? There are many, many times when you, as a system administrator, need...

Bash lowercase and uppercase strings

String data are used for different purposes in any bash commands or programming script. Sometimes we need to change the...