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.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

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.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

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.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.