Multiple Line Comments in Bash [2 Cases With Examples]

When you are writing a Bash script, you surround a block of code and you often want to disable or block some portions of code temporarily. Here, multiple-line commenting is the one that you need to pick to comment out those coded sections. This article will be helpful for you as I’m going to share the insights of multiple-line comments in Bash here. Let’s get into it.

Key Takeaways

  • Learning about multiple-line comments in Bash.
  • Getting ideas about some cases of multiple-line comments in Bash script.

Free Downloads

Context of Multiple Line Comments in Bash

In Bash script, multiple-line commenting refers to the way of commenting out multiple lines simultaneously either using a particular syntax or using a series of hashing symbols one by one. Multiple-line comments provide extensive explanations of a script and make it more approachable and vivid to other readers. Additionally, the compiler ignores these Bash comments during the script execution.

2 Cases of Multiple Line Comments in Bash Scripting

Multiple-line commenting is a little more expressive and organized than single-line commenting in a Bash script. Two points can describe multiple-line comments: i) Multiline comments, and ii) Block comments. The following section reveals a little overview of these two cases:

Case 1: Multiline Comments in Case of Multiple Line Comments

You can apply multiline commenting in your script by placing a series of single-line comments one after one. Practice the following steps to comment multiple lines out using the scheme of multiline commenting:

Steps to Follow >

➊ Open your Ubuntu Terminal.

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

nano multiline-multiple.sh
EXPLANATION
  • nano: A text editor.
  • multiline-multiple.sh: This is a script you can name by any choice. Here, I have named the script ‘multiline-multiple.sh’.
Opening the script in Nano text editor➌ Hereafter, write the following script inside the editor:

Script (multiline-multiple.sh) >

#!/bin/bash

#comment1
#comment2
#comment3
#To be continued…
#codes here
EXPLANATION

In #!/bin/bash, ‘#!’ is called ‘Shebang’. Then, ‘#comment1’, ‘#comment2’, #comment3’, ‘#To be continued…’, and ‘#codes here’, these lines indicate the multiple-line commenting scheme.

Multiline comments in case of multiple-line commentingThe above image shows how you can comment out multiple lines in a Bash script.

Case 2: Block Comments in Case of Multiple Line Comments

Another way to append multiple-line commenting in a Bash script is to use block comments. So, follow the terms below:

Follow the guide to learn more about How to Use Block Comments in Bash.

Script (block-multiple.sh) >

#!/bin/bash

: ‘
Comment out multiple lines
by using block comments
‘
EXPLANATION

Here, : ‘

Comment out multiple lines

by using block comments

remarks the commented-out code blocks within the single-quote delimiter (‘ ’) starting with a null command (:).

Block comments in case of multiple-line commentingFrom the image, you can see the commenting procedure for multiple lines where I have commented blocks of code at a time.

3 Practical Examples of Multiple-line Comments in Bash

In the following section, I’m going to introduce you to the practical execution of multiple-line comments in Bash script. So, let’s practice:

Example 1: Bash Multiple Line Comments for Disk Cleanup

You can comment out multiple lines in a Bash script for cleaning disk storage. Here is an example of multiple-line comments for disk cleanup:

Script (disk.sh) >

#!/bin/bash

#Performing disk cleanup
#It removes temporary & log files
#It frees up disk space

: '
Removing temporary files
rm -rf /tmp/*

Deleting log files
find /var/log -name "*.log" -delete
'
echo "Disk cleaning completed."
EXPLANATION

Here, ‘#Performing disk cleanup’, ‘#It removes temporary & log files’, and ‘#It frees up disk space’ indicates the multiple-line comments describing the purpose and function of the script. Then, the text starting with the null command (:) and inside the single quotation (‘ ’) displays some commented code block for deleting files & disk cleaning.

Multiple line comments for disk cleaning purposeIn the above image, you can see how I have commented multiple lines out using two different ways for disk cleanup.

Example 2: Bash Multiple Line Comments for Debugging

The debugging process gets easier by employing multiple-line comments within a script. Here is where multiple-line comments spark:

Script (multiline-debug.sh) >

#!/bin/bash

#Multiplication of two numbers
#Debugging mode- Uncomment the following line for debugging
#Set variables
var1=2
var2=9
#Displaying the value of var1
#Displaying the value of var2

#Multiply numbers
result=$((var1 * var2))

#Output of the result
echo “The product is: $result”
EXPLANATION

Here, The initial multiple-line comments #Multiplication of two numbers, #Debugging mode- Uncomment the following line for debugging, and #Set variables describe the purpose of the code. Then inside the codes, #Displaying the value of var1, #Displaying the value of var2 comments indicates the debugging process.

Multiple-line comments for debugging purposeThe highlighted multiline comments in the above image indicate the debugging process.

Example 3: Bash Multiple Line Comments for Checking Website Status

Multiple-line comments also help in checking the website’s status. Here’s how to check it:

Script (status.sh) >

#!/bin/bash

#Checking the status of a website
#It sends HTTP request
#It prints the HTTP response

: ‘
#Configuration
url=”http://linuxsimply.com”

#Send the request
response=$(curl -s -o /dev/null -w ‘%{http_code}’ “$url”)
echo “Response status: $response”
’
echo “Status checking completed.”
EXPLANATION

Here, ‘#Checking the status of a website’, ‘#It sends HTTP request’, and ‘#It prints the HTTP response’ are the multiple-line comments telling about the script’s purpose. Then, the comments inside the single quote delimiter and null command depict some portion of code that is commented out to check the status of the website by sending an HTTP request and printing the response.

Multiple line comments for checking website's statusFrom the above image, you can see the multiple-line comments that I have appended for checking the status of the website ‘linuxsimply.com’.

Conclusion

To conclude, multiple-line comments serve as an important annotation for a Bash script. These greatly improve the code readability and make the script easier for future use.

People Also Ask

Does Bash have any default syntax for multiple-line comments?
Actually, it depends. Which procedure you’re going to follow for multiple-line commenting in Bash will decide whether you need to use any particular syntax or not.
Can multiple line comments be nested up in Bash?
No, multiple line comments cannot be nested up in Bash. If you try this, it’ll cause a syntax error.

Do multiple-line comments affect the Bash script’s performance?
No, multiple-line comments don’t affect the Bash script’s performance as they are passive codes and are used only for the documentation of a script.

Can I write multiple-line comments in a conditional statement in Bash?
No, you cannot write multiple-line comments within a conditional statement directly. But you can comment out the entire conditional block using multiple-line comments.

Are single-line comments easy to use than multiple-line comments in Bash script??
Multiple-line commenting in Bash script is not too difficult to append. But comparatively, single-line comments are easier to use script than multiple-line comments because of their simple syntax.

Related Articles


<< Go Back to Bash Comments | Bash Scripting Tutorial

Rate this post
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