Bash Multiline Comment in Ubuntu

  • Post author:
  • Post comments:0 Comments
  • Reading time:3 mins read
Bash Multiline Comment in Ubuntu

Every Linux user or server admin loves to write a bash script in a way that is clean and easily understandable to him/her or anybody who is going to read that code. We can obtain this by doing multiple things like indenting, giving a proper name to variable and functions, or by using bash multiline comment or single-line comment.

What is Bash Multiline Comment?

A comment is a human-readable explanation that helps in the readability of a program. While executing the bash script comments are ignored by the bash shell.

Bash only provides single-line comments but using some hacks we can also do multiline comments in a bash shell script.

The single-line comments can be done very easily using hash (#) before any line, but there are multiple ways to do multiline comment.

In this tutorial, we are going to see the ways to do bash multiline comments in Ubuntu.

Let’s See Some Examples of Bash Multiline Comment

Example 1:- Using Here Document

Here Document is a type of redirection that allows us to pass multiple lines of input to a command. If it’s not redirected to any command, then it can be used as a multiline comment block.

#!/bin/bash

<< Multiline_Comment
This Lines Will be
Commented Out
Multiline_Comment

echo "Ubuntu Config!"
Bash multiline comment Using Here Document

When we execute the above script, it will only print “Ubuntu Config!“. All the content between <<Multiline_Comment and Multiline_Comment block will be ignored.

The output of the above code is given below.

Output of multiline comment Using Here Document

Example 2:- Using “:” Command

Another example of multiline commenting is using the “:” command. In this, we just need to write our multiline comment in single quotes after “:” as seen below.

#!/bin/bash

: '
This Lines Will be
Commented Out
'

echo "Ubuntu Config!"
Bash multiline comment Using ':' command

When we execute the above script, it will only print “Ubuntu Config!“. All the content between: and block will be ignored.

The output of the above code is given below.

Output of multiline comment using : ' ' .

Example 3:- Using Hash (#)

This is the simplest way to add multiline comments in bash. In this, we just need to put a hash (#) before every line we want to comment the same as we do in a single-line comment as seen below.

#!/bin/bash

#This Lines Will be
#Commented Out

echo "Ubuntu Config!"
Bash multiline comment Using hash (#).

When we execute the above script, it will only print “Ubuntu Config!“. All the content starting with a hash (#) will be ignored.

The output of the above code is given below.

Output of multiline comment using hash(#).

Conclusion

In this tutorial, we have learned how to do bash multiline comments in Ubuntu.

Leave a Reply