Bash Comments: Single-Line, Multiline, and Best Practices

By 

Updated on

5 min read

Bash Script Comments

When writing Bash scripts, it is always a good practice to keep your code clean and easy to understand, which is essential for maintainability. You can achieve this by organizing code into logical blocks, using proper indentation, and choosing descriptive names for variables and functions.

One of the most effective ways to make your scripts understandable is to add meaningful comments. A comment is a human-readable explanation or annotation written within the shell script that Bash ignores during execution.

Why Comments Matter

Adding comments to your Bash scripts will save you a lot of time and effort when you look at your code in the future. If you want to modify a script you wrote months or years ago, you may not remember why you wrote a particular piece of code unless you added a comment.

Comments also help your teammates, collaborators, and system administrators quickly grasp the script’s purpose and logic without having to trace through every line.

When you have a complex regex or parameter substitution inside your script, a short comment describing what the code does is far more useful than a long explanation. Keep comments short and to the point — avoid explaining something that is already clear and straightforward to the reader.

How Bash Handles Comments

Bash treats everything from a # symbol (hash mark) to the end of the line as a comment and ignores it during execution.

The only exception is when the first line of the script starts with the #! characters. This sequence is called a shebang and tells the operating system which interpreter to use to parse the rest of the file.

The # comment syntax works the same way in other POSIX-compatible shells such as sh and zsh, so everything in this guide applies to shell scripts in general.

Single-Line Comments

Comments can be added at the beginning of a line or inline with other code:

sh
# This is a Bash comment.
echo "This is Code" # This is an inline Bash comment.

The blank space after the hash mark is not mandatory, but it improves readability.

If your text editor supports syntax highlighting, comments are usually displayed in green or grey.

Comments are also useful when testing a script. Instead of deleting lines or blocks, you can comment them out:

sh
# if [[ $VAR -gt 10 ]]; then
#  echo "Variable is greater than 10."
# fi

Multiline and Block Comments in Bash

Unlike most programming languages, Bash has no built-in multiline or block comment syntax. There are two common approaches.

The simplest and most portable way to write a multiline comment is to prefix each line with #:

sh
# This is the first line.
# This is the second line.

This is the recommended approach. It is clear, works in every shell, and editors with syntax highlighting will color each line as a comment.

HereDoc Block Comment

Another option is to use a HereDoc block that is not redirected to any command. Because nothing reads the input, it acts as a block comment placeholder:

sh
<< 'MULTILINE-COMMENT'
    Everything inside the
    HereDoc body is
    ignored by Bash
MULTILINE-COMMENT

This is a workaround, not a real built-in feature. The single quotes around the delimiter ('MULTILINE-COMMENT') are important — they prevent Bash from expanding variables and commands inside the block. To avoid unexpected behavior, prefer single-line comments for all normal commenting needs.

Best Practices

  • Use # followed by a space for readability: # comment, not #comment.
  • Write comments that explain why, not what — the code already shows what it does.
  • Add a header comment at the top of every script describing its purpose, author, and usage.
  • Comment out code during debugging rather than deleting it, so you can restore it easily.
  • Keep comments up to date — an outdated comment is worse than no comment.

Quick Reference

SyntaxUse case
# commentSingle-line or end-of-line comment
echo "code" # commentInline comment after a command
# line1 / # line2Multiline comment (recommended)
<< 'EOF' ... EOFHereDoc block comment (workaround)
#! on line 1Shebang — not a comment, sets the interpreter

For a printable quick reference, see the Bash cheatsheet .

FAQ

What is the correct syntax for a Bash comment? Use the # character. Everything from # to the end of the line is ignored by Bash. Example: # This is a comment.

Does Bash support block comments? Not natively. The most reliable way to write a block comment is to use multiple single-line comments, each starting with #. The HereDoc trick (<< 'EOF' ... EOF) works but is not a real language feature.

Can I comment out multiple lines at once? Most text editors support block-comment shortcuts (for example, select lines and press Ctrl+/ in VS Code). In the terminal, prefix each line manually with #.

What is the difference between # and #!? # starts a comment anywhere in a script. #! on the very first line is a shebang — it tells the operating system which interpreter to run the script with (for example, #!/bin/bash). The shebang is not treated as a comment by the OS.

Should I use HereDoc for block comments? Only if you need to temporarily disable a large block and your editor does not support multi-line comment shortcuts. Always quote the delimiter ('EOF') to prevent variable expansion inside the block. For normal commenting, stick to #.

Conclusion

In Bash, the # character marks everything that follows it on the same line as a comment. For multiline blocks, use consecutive single-line comments — they are the clearest and most compatible approach. Well-commented scripts save time, reduce bugs, and make maintenance easier for everyone who works with them.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page