How to Add Comments in Shell Scripts with Examples

When working with shell scripts, adding comments is crucial for making your scripts easier to understand, maintain, and debug. Whether you are a beginner or an experienced Linux admin, understanding how to add comments in shell scripts is a fundamental skill in shell scripting.

In this blog post, you will learn the proper shell script comment syntax, including both single-line and multi-line comments, along with practical examples.

Why Add Comments in Shell Scripts?

Adding comments to your shell scripts has many advantages like:

  • Makes your code more readable and self-explanatory
  • Helps with debugging by allowing you to isolate code
  • Acts as documentation for yourself or other team members
  • Useful in scripts used by multiple people or in production environments

Single-Line Comments in Shell Scripts

The most commonly used form of commenting in shell scripts is the single-line comment. It starts with a hash symbol `#`. Use single-line comments to explain what a specific line or block of code does.

Syntax:

# This is a single-line comment
echo "Hello Techies"  # This is an inline comment

Example:

$ vi greet.sh
#!/bin/bash
# Print the current username
user=$(whoami)

# Greet the user
echo "Hello, $user!"

save and close the file and then execute the script

$ chmod +x greet.sh
$./greet.sh

Single-Line Comments in shell scripts

Multi-Line Comments in Shell Scripts

Unlike some programming languages, shell scripts don’t have a built-in multi-line comment syntax. However, you can achieve the same effect using one of the following methods:

 Method 1: Multiple ‘#’ Symbols

Just use ‘#’ at the beginning of each line:

#!/bin/bash
# This script shows a welcome message
# It also displays the current date and time
# and the logged-in username

Method 2: Use ‘: << ‘COMMENT”

This trick works by redirecting input to a no-op command `:`:

#!/bin/bash
: << 'COMMENT'
This is a multi-line comment
Used across the various continues lines
It will be ignored by the shell.
COMMENT

Note: Avoid this in loops or conditionals where behavior might vary in older shells.

Method 3: Add Comments in shell script Using Heredocs

Heredocs are another popular way to add multi-line comments in Bash scripts. This approach is similar to the `: << ‘COMMENT’` method, but uses the `<<` operator directly without the no-op `:`. It can offer slightly more flexibility depending on the shell and use case.

Example:

$ vi multiline-comments.sh
#!/bin/bash
<<COMMENT
This is a heredoc comment.
It can also span multiple lines.
This text will not be executed.
echo “Welcome to Comments Section”
COMMENT

name=$(hostname)
echo "HOSTNAME=$name"
if [ $? -eq 0 ]; then
   echo "Welcome to Shell Script World"
else
   echo "Something is wrong, please correct it"
fi

save and close the file.

This method effectively skips the text block enclosed between `<<COMMENT` and `COMMENT` during execution, treating it as a comment block.

multi-line comments in shell scripts

Shell Script Example with Comments

Below is a simple bash script comments example showing both types of comments:

$ vi comments-script.sh
#!/bin/bash
# This script greets the user and shows the current date and time
<<  COMMENT
Author: Your Name
Date: 2025-04-12
Description:
This shell script prints a welcome message,
greets the user, and displays the current date.
COMMENT

# Get the current username
user=$(whoami)

# Display welcome message
echo "Hello, $user!"
# Display current date and time
echo "Current date and time: $(date)"

save and close the file.

Shell Script Example with Comments

That’s all from blog post, I hope you found it useful and informative. Feel free to post your queries and feedback in below comments section.

Conclusion

Now that you know how to add comments in shell scripts, make sure to use them wisely to improve the readability and maintainability of your code. Whether it’s a simple one-liner or a full script with documentation, comments are essential in shell scripting for beginners and experts alike.

Also Read: How to Use If Statement in Bash Scripting

Leave a Comment

Your email address will not be published. Required fields are marked *