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
- 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’.
Script (multiline-multiple.sh) >
#!/bin/bash
#comment1
#comment2
#comment3
#To be continued…
#codes here
In #!/bin/bash, ‘#!’ is called ‘Shebang’. Then, ‘#comment1’, ‘#comment2’, #comment3’, ‘#To be continued…’, and ‘#codes here’, these lines indicate the multiple-line commenting scheme.
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:
Script (block-multiple.sh) >
#!/bin/bash
: ‘
Comment out multiple lines
by using block comments
‘
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 (:).
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."
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.
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”
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.
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.”
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.
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
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]
- How to Use Block Comment in Bash? [2 Cases]
<< Go Back to Bash Comments | Bash Scripting Tutorial