How to Comment Effectively in Bash Scripting?
Introduction
Have you ever found yourself struggling to comprehend a code or script? The concept of “commenting” refers to the practice of providing explanatory notes within the script to help the reader understand its logic.
In today’s world, where scripting languages like Pearl, Javascript, and Bash are increasingly prevalent, the ability to comprehend scripts is more important than ever before. Although scripting is a subset of coding, it performs specific tasks by following instructions. Bash, on the other hand, is a scripting language, interpreter, and Unix shell all in one. It is used for automating repetitive tasks and managing system administration. By incorporating a comment in Bash scripting, it becomes much easier to understand, and there are numerous advantages to doing so.
In this article, we will enlighten you about Bash scripting, including the proper techniques for commenting. So read on to attain a more in-depth wisdom of this valuable feature.
How to Comment in Bash?
Commenting in shell script makes a code squeaky clean and comprehensible. We will guide you toward the basic way how to comment in Bash.
The most important thing about comments in a bash script is that it is denoted by a “#.”; Everything that is written after this symbol in a shell script is a comment. Another important point is that there is no bar on the location of the comment; the “#” symbol can be placed anywhere around the bash script. Examine the below example to get a good idea about the placement of the symbol.
# This is a Bash comment.
echo “This is Code” # This is an inline Bash comment.
(This is an example of a single line and inline Bash comment)
A point to note about the above example is that the placement of a blank after the symbol is not important, and it is used just for readability purposes. Moreover, another important term is used for Bash script commenting, i.e., “comment out” Bash. This term is used to make a code inactive. If you want your interpreter (a program that executes your script without compiling it into machine language) to ignore the code for some time, then simply place”#” before the code and write it. Have a glance at the example in the comment below.
# This is a commented-out block
# echo “Line 1”
# echo “Line 2”
# echo “Line 3”
(This is an example of a multiline Bash comment)
Steps to comment in Bash script
This is a short guide to creating a single-line Bash comment.
Step 1: Start the interface through CTRL+ALT+T and curate a script using your preferred text editor (Vi, nano, Emacs, Atom, etc.)
vi comment.sh
Step 2: Add the below-mentioned code to your script on the interface.
# A comment is considered a single line if you do not press Enter. Below is the Shebang, indicating the script uses the bash shell.
#!/bin/bash
# This is a single-line comment above a command.
echo “Hello world!” # This is an inline comment.
# This is a single-line comment below a command.
Let’s understand the above code stating each “#” as a line.
Line 1: This is a single-line comment in the Bash file.
Line 2: This line denotes the Shebang that instructs to use a Bash interpreter.
Line 3 and 5: Both lines are single-line comments placed before and after a command.
Line 4: If you see closely, this is a single-line comment placed inline with an echo command (this command is used to display a message on the interface when the script is executed)
Best Practices and Tips for Bash Comments
We have curated a quick list of all those tips that will give you good results in terms of comments in a shell script.
- Always incorporate a file header to display the correct information about the script, like the purpose and authorship.
- Say no to “block comments” because it disturbs the harmonious syntax of the commenting part.
- Avoid unimportant comments to keep your code crisp and clean, rather than messy and confusing.
- Ensure labeling to categorize the comment properly. Examples of labeling could be todo, fix me, note, etc.
- Don’t forget to include the “#” symbol for each comment; otherwise, it will all go in vain, and the interpreter will find an error.
- Use Shebang to point out the specific interpreter. Shebang is essential when you have multiple versions of the interpreter installed on your system.
Also Read: Steps to Unzip / Extract tar.gz Files in Linux Using Command Line
What is the need to include the comment in Bash Scripting?
A comment in Bash is very useful to all those people who are using a script or code after a long time. The comment or explanation will make it easier for them to understand the purpose or logic behind the script. Moreover, it will save them valuable time as the understanding process will be quick. The comment in the shell script will enable them to alter the script easily.
Conclusion
The crux of this article is that a comment in the bash file is very important to make the script understandable even after a long time. Such comments are denoted by “#” in Bash scripting, which is a scripting language. A key point to note here is that a comment can be inline, single, or placed in multiline as per the requirement. If you want to make the best use of your comments, then ensure the mitigation of blocked comments and unimportant comments. Moreover, try to include a file header to make your script prominent, readable, and clean.