FUNDAMENTALS A Complete Guide for Beginners
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
- 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’.
➌ 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.”
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.
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!"
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.
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
Related Articles
- 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]
<< Go Back to Bash Comments | Bash Scripting Tutorial