How to Add Comments in Your Bash Scripts

When you write a Bash script, sometimes you want to add comments to explain what certain parts of the script do.

In Bash scripting, there are several ways to add comments, and in this article, we’ll explore different methods to do just that.

1. Single Line Comment in Bash

The most common way to add comments in Bash scripts is by using the # symbol. Anything that comes after the # on a line is considered a comment and is ignored by the Bash interpreter.

Here’s an example:

#!/bin/bash

# This is a comment
echo "Hello, World!"

In this script, ‘# This is a comment‘ is a comment, and it doesn’t affect the execution of the script.

Single Line Comment in Bash
Single Line Comment in Bash

2. Inline Comments in Bash

You can also add comments on the same line as a command using the # symbol, as these comments appear alongside the command or code statement.

Here’s an example:

#!/bin/bash

echo "Hello, World!"  # This is an inline comment
Inline Comments in Bash
Inline Comments in Bash

3. Multi-line Comments in Bash

In Bash scripting, the language doesn’t have a built-in multi-line comment syntax like some other programming languages.

However, you can simulate multi-line comments by placing a # at the beginning of each line you want to comment out.

Here’s an example:

#!/bin/bash

# This is a multi-line comment.
# It spans across several lines.
# These lines will not be executed.

echo "Hello, World!"
Multi-line Comments in Bash
Multi-line Comments in Bash

4. Multi-Line Comments with “: <<” Operator

You can also create multi-line comments using the : << operator, which allows you to add comments spanning multiple lines.

Here’s an example:

#!/bin/bash

: <<'COMMENT'
This is a multi-line comment.
It can span several lines.
This text will not be executed.
COMMENT

echo "Hello, World!"
Multi-Line Comments with ": <<" Operator
Multi-Line Comments with “: <<” Operator

5. Add Comments in Bash Using Heredocs

Heredocs are another way to add comments in Bash scripts, as they are similar to the : << method but offer more flexibility.

Here’s an example:

#!/bin/bash

<<COMMENT
This is a heredoc comment.
It can also span multiple lines.
This text will not be executed.
COMMENT

echo "Hello, World!"
Add Comments in Bash Using Here Documents
Add Comments in Bash Using Here Documents

6. Add Comments in Bash Using : as a No-Op Command

In Bash, the ":" symbol is a no-op command that does nothing. You can use it to add comments in your script.

Here’s an example:

: This is a comment using the no-op command
echo "Hello, World!"
Add Comment in Bash Using No-Op Command
Add Comment in Bash Using No-Op Command
Conclusion

Adding comments to your Bash scripts is essential for improving readability and understanding. Whether you use the # symbol for single-line comments, the : << operator for multi-line comments, heredocs, or inline comments, it’s crucial to document your code effectively.

By choosing the right method for your comments, you can make your Bash scripts more understandable for yourself and others who might read your code.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.