Bash Shell Script Comment

Linux Bash Shell Script Comments can be used to add some information about the script of specific command or line in a script file. Comments are not interpreted as a shell script command. Single line or multiline comments can be created in a shell script.

Bash Shell Script Singleline Comment

The hash mark (#) is used to create single-line comments in a shell script. Single line comments are useful to explain shell script variables, functions, and other similar basic things. In the following shell script, we add comments for every line to explain it.

#!/bin/bash

# Print information about start
echo "Starting Operation"

# The first variable set to 1
x=1

# The second variable set to 2
y=2

# The two variables sum is calculated and result is assigned to z
z=$(($x+$y))

# Print the result
echo $z

# Print information about end
echo "Operation Completed"

Bash Shell Script Multiline Comment

Bash shell provides multiline comments in order to explain things in more details by using multiple lines. The <<COMMENT is used to set start of the multiline comment and COMMENT is used to set end of the multiline comment. All characters between these start and end delimiters are interpreted as comment and not executed as bash shell command.

#!/bin/bash


<<COMMENT
This script is created to provide function to summaries two values.
The printed value is the result of the summary.
This is a multiline or block shell script comment.
COMMENT

# Print information about start
echo "Starting Operation"

# The first variable set to 1
x=1

# The second variable set to 2
y=2

# The two variables sum is calculated and result is assigned to z
z=$(($x+$y))

# Print the result
echo $z

# Print information about end
echo "Operation Completed"
Bash Shell Script Multiline Comment

From the screenshot we can see that bash shell comments are printed with different color.

Bash Shell Script Multiline Comment with Hash Mark

The hash mark is designed to create single-line comments but they can be used multiple times to create multiline or block comments. But in order to make a multiline comment, every line should be started with a hash mark. In the following example, we create multiline comments by single the hash mark.

#!/bin/bash


#This script is created to provide function to summaries two values.
#The printed value is the result of the summary.
#This is a multiline or block shell script comment.


# Print information about start
echo "Starting Operation"

# The first variable set to 1
x=1

# The second variable set to 2
y=2

# The two variables sum is calculated and result is assigned to z
z=$(($x+$y))

# Print the result
echo $z

# Print information about end
echo "Operation Completed"
Bash Shell Script Multiline Comment with Hash Mark

Leave a Comment