How to Use Block Comment in Bash? [2 Cases]

It’s so obvious to comment out some specific parts of code while writing a Bash script. No doubt that single-line comments are way too easy to implement. But what to do if you need to comment out some blocks of code? Well, you can use block comments in this case as these will add an organized texture in your Bash script. In this article, I am going to share how you can use a Bash block comment in different ways. So, let’s dive into it!

Key Takeaways

  • Getting basic ideas about block comments in Bash.
  • Utilizing the practical cases of Bash block comments.

Free Downloads

What is Block Comment in Bash?

Bash block comments refer to the multiple-line comments that can enclose blocks of codes using a specific syntax with a single code marker. These Bash comments are for optimizing the program’s readability. The default interpreter always ignores these blocks of comments during the script execution.

2 Cases to Use Block Comments in Bash

When writing a Bash script, it’s very important to add comments to illustrate the function of the code clearly. Generally, comments are remarked by the hash(#) symbol in a Bash script. But if you want to add block comments within your script, then you have to follow some other syntax.

In the following section, I’ll describe the two possible methods to create block comments in a Bash script:

Case 1: Representation of Bash Block Comment Using the “here Document” Notation

The first depiction of the Bash block comment is to use the ‘here document’. Follow the steps given below to write block comments in Bash using the Here document:

Steps to Follow >

➊ Open your Ubuntu Terminal.

➋ To open a script in the nano text editor, write the command below:

nano here-doc.sh
EXPLANATION
  • nano: A text editor.
  • here-doc.sh: This is a script you can name by any choice. Here, I have named the script ‘here-doc.sh’.

Opening the script in Nano text editor➌ Afterward, write the following script inside the text editor:

Script (here-doc.sh) >

#!/bin/bash

: <<'comment'
This is a block comment
that is represented by Here document
and it won’t be executed.
comment
echo “Hello, This is not a comment.”
EXPLANATION

Here, in #!/bin/bash, ‘#!’ is called “Shebang or Hashbang”. Then, the section,

“This is a block comment

that is represented by Here document

and it won’t be executed.”, represents a block comment that starts with the syntax, ‘: <<‘comment’’ of a Here document where the comment before the echo command remarks the enclosing point of the Here document.

Representing Bash block comments using the 'Here document' notationYou can see that the above image discloses a block comment in Bash that I have written with a Here document syntax.

Note: The ‘here document (heredoc)‘ refers to a special code block with a specific format of implementation.

Case 2: Representation of Bash Block Comment Using the “: (Null)” Command

By using a null command (:), you can include block comments in a Bash script. So, follow the section below to create a Bash block comment:

Script (null.sh) >

#!/bin/bash

: '
This is a block comment
and this describes the purpose of the script
'
echo "Hello, Bash!"
EXPLANATION

Here, in the section,

“: ‘

This is a block comment

and this describes the purpose of the script

‘”, the ‘:’, a null command, does nothing to the code and is only used to create a block comment that spans two lines enclosed by single quotes.

Representing Bash block comments using null (:)commandThe above image you can see is a portrayal of the Bash block comment which the interpreter does not execute.

Conclusion

Comments are the most essential part without which your script will be incomplete. Precisely saying, by following the different schemes discussed in the article, you can easily create concise block comments in a Bash script.

People Also Ask

Does Bash require a dedicated format for block comments?
Yes, there are some specific formats to write block comments in a Bash script.

Can I write block comments like /* ... */ in Bash?
No, you cannot write block comments like /* …*/ as Bash doesn’t support this kind of format.

Can I nest block comments within Bash script?
No, you cannot nest block comments within Bash script because the syntax used for block commenting, generally comments out the entire section.
What is the difference between inline and block comments?
Inline and block comments are way too different. Inline comments are short comments added on the same line as code while block comments are longer than inline comments and can break among multiple lines.

Can I disable some portions of code temporarily using the Bash block comments?
Yes, using the block comments, you can disable some portions of code temporarily in a Bash script without deleting the code.

Related Articles


<< Go Back to Bash Comments | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment