How to Add Comments in Your Bash Scripts

Take Your Linux Skills to the Next Level All courses, certifications, ad-free articles & community — from $8/mo
Join Root →
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
From $8/mo · or $59/yr billed annually · Cancel anytime

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.

Root Plan
Premium Linux Education for Serious Learners

Take Your Linux Skills to the Next Level

Root members get full access to every course, certification prep track, and a growing library of hands-on Linux content — with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
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.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

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.

Root Plan Premium Linux Education for Serious Learners

Before You Go - Upgrade Your Linux Skills

Root members get everything in one place, with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Linux certifications: RHCSA, RHCE, LFCS and LFCA
Access new courses on release
Weekly newsletter, priority support & Telegram community
Join Root Today and Start Learning Linux the Right Way
Structured courses, certification prep, and a community of Linux professionals - all in one membership.
Join Root Plan →
$8/mo · or $59/yr billed annually