Oct 24, 2023 3 min read

Writing Comments in Bash Scripts

Learn to write comments in Bash Scripts with our step-by-step tutorial. Bash Script is a plain text file which contains a series of commands.

Writing Comments in Bash Scripts
Table of Contents

Introduction

Before we start talking about writing comments in Bash Scripts, let's briefly understand-What is Bash Script ?

A Bash script is a plain text file which contains a series of commands. Writing clear and straightforward code is usually recommended when working with Bash scripts. It may be done in several methods, including by using blocks, indentation, and descriptive names for variables and functions.

The use of comments is another technique for making code easier to understand. A remark is an explanation or annotation in the shell script that can be read by a person.

In this tutorial, we’ll cover the basics of writing comments in Bash. We will also address a few FAQs on writing comments in Bash Scripts.

Writing Comments in Bash

The content of a line following a hash mark (#) is disregarded by Bash. The only time this doesn't apply is if the first line of the script contains the characters #!. The purpose of this string of characters, known as the Shebang, is to instruct the operating system on which interpreter to employ when interpreting the remainder of the file.

Comment lines may be inserted at the beginning of a line or inline with other code.

# This is a Bash comment.
echo "This is Code" # This is an inline Bash comment.

The space following the hash mark is optional but recommended for better reading of the remark.

Comments are often shown in green if your text editor supports syntax highlighting.

Testing a script with comments is helpful as well. You may comment out lines or blocks instead of removing them:

# if [[ $VAR -gt 10 ]]; then
#  echo "Variable is greater than 10."
# fi

Multiline Comments in Bash

Bash does not allow multiline comments, unlike the majority of computer languages.

Adding single comments one after another is the easiest approach to creating multiline comments in Bash:

# This is the first line.
# This is the second line.

Another option is to utilize HereDoc. It's a kind of redirection that lets you feed a command with a string of input lines. In the absence of command redirection, the HereDoc block may be used to initiate several lines of commentary.

<< 'MULTILINE-COMMENT'
    Everything inside the
    HereDoc body is
    a multiline comment
MULTILINE-COMMENT

Using HereDoc to produce multiline Bash comments is a workaround rather than a built-in feature. Use single-line comments wherever possible to prevent problems.

FAQs for Writing Comments in Bash Scripts

Can I comment out lines of code temporarily? 

Yes, comments are often used to temporarily disable lines of code during debugging or testing.

Is there a limit on the length of a comment? 

No, there is no specific limit on the length of a comment. However, it is good practice to keep comments concise and focused.

Can I nest comments in Bash scripts? 

No, in Bash, comments cannot be nested. Each comment must be closed before starting another.

Is there a specific place where comments should be placed in the script? 

Comments can be placed anywhere in the script, ideally near the relevant code or at the beginning of a section to provide an overview.

Can I write comments in any language? 

Yes, comments can be written in any language for better understanding by developers. However, it is recommended to use comments in English for wider accessibility.

Should I comment every line in my script?

Commenting every line is not necessary if the code itself is clear and self-explanatory. Comments should be used judiciously for areas that require additional explanation.

Conclusion

It is excellent practice to include comments to your shell scripts so that other developers, and maybe even your future self, can comprehend them. Bash interprets text following the hash symbol (#) up to the line ending character as a remark.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.