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!
- Getting basic ideas about block comments in Bash.
- Utilizing the practical cases of Bash block comments.
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:
➌ Afterward, write the following script inside the text editor:
Script (here-doc.sh) >
You can see that the above image discloses a block comment in Bash that I have written with a Here document syntax.
This is a block comment
that is represented by Here document
and it won’t be executed.
echo “Hello, This is not a comment.”
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) >
The above image you can see is a portrayal of the Bash block comment which the interpreter does not execute.
This is a block comment
and this describes the purpose of the script
echo "Hello, Bash!"
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
- The Art of Commenting in Bash
- Bash the Hash! – a Symbolic Feature of Bash Comment
- What Are Single-line Comments in Bash? [2 Cases With Examples]
- Multiple Line Comments in Bash [With Shortcut Keys]