Skip to main content
Shell Scripting:

How to add comments in Bash scripts

Summary

This article covers the different ways to write comments in Bash scripts, including single-line, multi-line, and inline comments. It also discusses the special exception of the shebang (#!) line.

Introduction #

Comments in Bash scripts are used to explain the logic, clarify complex sections, and disable specific lines during testing. Bash uses the hash symbol (#) to mark text as a comment, which is then ignored by the interpreter. Everything after a # in a line is interpreted by Bash as a comment.

Single-line comments #

To add a single-line comment in a Bash script, use the hash symbol (#) at the beginning of the line. Everything following the # on that line will be treated as a comment.

# This is a single-line comment
echo "Hello, world!"

Single-line comments are useful for describing the purpose of a command or a block of logic.

Multiple-line comments #

Bash does not have a built-in syntax for block comments like other programming languages. To comment multiple lines, you must prefix each line with #.

# This block explains the next command
# It updates the system's package index
# and installs updates
sudo apt update
sudo apt upgrade

Alternatively, you can simulate a block comment using a here document with a no-op command like :. This is not a true comment, but it prevents the enclosed lines from being executed.

: <<'END_COMMENT'
This is a simulated block comment.
These lines will not be executed.
END_COMMENT

Inline comments #

An inline comment is placed on the same line as a command, following a # symbol. Anything after the # will be ignored.

rm -rf /tmp/cache  # Remove temporary cache directory

Be sure to leave a space before the # to improve readability and avoid confusion with command syntax.

The #! shebang is not a comment #

The shebang line starts with #! and is typically the first line in a script. It specifies the path to the interpreter that should execute the script.

#!/bin/bash

Although the line begins with a #, it is not treated as a comment. The operating system uses the shebang to determine which interpreter to invoke. If the shebang is omitted, the default shell may be used, which can lead to unexpected behavior.

FAQ's #

Most common questions and brief, easy-to-understand answers on the topic:

Can I use C-style comments like /* comment */ in Bash scripts?

No. Bash does not support C-style block comments. You must use the hash symbol (#) for commenting.

Is the shebang line #!/bin/bash a comment?

No. Although it starts with #, the shebang is not treated as a comment. It tells the system which interpreter to use to execute the script.

How do I comment out multiple lines in Bash?

You can add a # at the beginning of each line. Alternatively, you can use a here document inside a conditional block to simulate block comments.

Do comments affect script performance?

No. Comments are ignored by the Bash interpreter and do not impact the performance or execution of the script.

Can I place a comment after a command on the same line?

Yes. This is called an inline comment. Anything after the # on the same line will be treated as a comment.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

How to add comments in Bash scripts by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-add-comments-to-bash-scripts">How to add comments in Bash scripts</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the Ditig legal page.

All Topics

Random Quote

“When providing information, make sure your site has something unique to offer.”

Sally Hambridge Chair of the IETF Network Working GroupIETF RFC 1855 - Netiquette Guidelines, - IT quotes