How to Write Comments in Bash Scripts

This article explains how you can write comments in your bash scripts to make the code more readable and help other developers understand your code.

Writing Comments in Bash Scripts
Writing Comments in Bash Scripts

Bash has been a reliable companion for system administrators and developers who use it for scripting and automation. It has become popular due to its efficiency and versatility when writing scripts. However, developers and admins will at times write long scripts and things could easily get out of hand as the scripts grow in complexity. In this situation, maintaining clarity becomes a problem and one way to overcome it is with the use of comments.

Comments are often underrated but they make a huge difference in maintaining the readability of your scripts. They act as signposts to guide you and others that may interact with your code in the future.

In this article, you will learn the art of writing comments in Bash Scripts. Let's get started.

Writing Comments in Bash

To add a single-line comment to your scripts, use the hash symbol # followed by the comment describing what the code does. Bash will ignore everything that comes after the # symbol on the line with the exception of the Shebang which has the characters #!.


# This is the first single line Comment
echo "Geekbits"

If you run the script above, you will notice that only the string Geekbits prints to the terminal.

Inline Comments in Bash

You can also add the comments in line with the other code.


echo "Subscribe to geekbits" # This is an inline comment.

Multi-line Comments in Bash

Bash does not have a built-in multi-line comment syntax, so the easiest to add multi-line comments in bash is by adding the hash symbol # to every line you want to comment.


# This script is used to check if a variable is empty
# I am using the hash symbol to add multi-line comments
# You can find out more about this example below. 

if [ -z $url]; then
    echo "Variable is empty"
    echo "value: $url"

This example is used to check if a variable is empty. Find out more in this article.

How to Check If Bash Variable is Empty
For this tutorial, we are going to explore various methods and techniques to determine whether a given bash variable is empty or not.

Using the : command for multi-line commenting

Another workaround is to use the : followed by the sing quote. The comments are added in between the single quotes. Here is an example.

: '
All the content here is regarded as a comment 
You can add as many lines of comment as you want.
As long as they stay inside the single quotes

Using the heredoc for multi-line commenting

A here document is a feature in Bash that allows you to include multiple lines of input within a script without the need for external files. Here is how it works.

  • Add the << operator followed by a unique delimiter. The delimiter can be any string of characters that you choose.
  • Use the delimiter you have chosen to mark the end of the comment.


	This is a comment
	This comment has been achieved by the use of heredoc
	You can add as many lines of comments as possible.

In this example, The delimiter used is the string "Geek". Notice that it comes after the << operator and closes the comment at the end.


To sum up, comments are very useful in development. They explain the code that may be difficult to understand at first glance. This is helpful for other people that may look into your work and it would also help you if you check your code after a long time. Comments can also be used to temporarily disable sections of code for testing purposes. This article shows how you can use comments in Bash Scripts. If you found the article helpful, make sure to subscribe and share it with interested parties.

Thanks for reading : )

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.