Bash Comments: What the Shell Ignores

Bash comments are notes written inside a shell script to explain commands, variables, decisions, or temporary changes. A comment is ignored by Bash, so it does not run as a command and does not affect the output of the script.

The usual Bash comment starts with the hash symbol #. The main exception is the HashBang line, such as #!/bin/bash, at the beginning of a script. That line also starts with #, but it tells the operating system which interpreter should run the script.

In this tutorial, we shall learn how to write single line comments, inline comments, and practical multiline comment blocks in a Bash script file.

Bash Single Line Comments Using #

To write a single line comment in Bash, start the line with the hash symbol #. Bash ignores everything from # to the end of that line, provided the symbol is not inside quotes and is not escaped.

Single line Bash comments are useful for explaining the purpose of a script, marking sections, describing variables, or adding a short note before a command.

In the following example, we write single line comments that span the whole line and single line comments after a Bash statement.

Bash Script File

</>
Copy
#!/bin/bash

# this is a single line comment in bash
echo Learn Bash Scripting

a=2 # this is also a comment, but after the command in same line
b=4
# addition : this is another single line comment
c=$(($a + $b))

# echo result to console
echo $a + $b = $c

Output

~$ ./bash-single-line-comments-example 
Learn Bash Scripting
2 + 4 = 6

Bash Inline Comments After Commands

An inline comment appears after a command on the same line. It is commonly used to explain a value or a small command without adding a separate comment line.

Place at least one space before # when using an inline Bash comment. This keeps the command readable and avoids confusion in places where # may be part of a value.

</>
Copy
#!/bin/bash

backup_days=7      # keep backup files for 7 days
log_file="app.log" # log file used by the script

echo "Keeping backups for $backup_days days"
echo "Writing logs to $log_file"

Inline comments are helpful for short notes. For longer explanations, prefer a separate comment line above the command.

Bash # Inside Quotes Is Not a Comment

A hash symbol inside single quotes or double quotes is treated as normal text. Bash does not treat it as the beginning of a comment.

</>
Copy
#!/bin/bash

echo "This # is printed because it is inside double quotes"
echo 'This # is also printed because it is inside single quotes'

echo Done # this part is a comment

Output

This # is printed because it is inside double quotes
This # is also printed because it is inside single quotes
Done

Bash Multiline Comments with a Here Document

Bash does not have a dedicated multiline comment syntax like some programming languages. However, multiline comments are often written using a here document with a command that does nothing. The common form uses <<COMMENT and ends with the same marker name.

In the following example, we write multiline comments using <<.

Bash Script File

</>
Copy
#!/bin/bash

# this is a single line comment in bash
echo Learn Bash Scripting

<<COMMENT
	This is a multiple line comment
	In Bash Scripting
COMMENT
echo Good Day!

<<COMMENT
	This is a multiple line comment
	End of the example
COMMENT

Output

~$ ./bash-multiple-line-comments-example 
Learn Bash Scripting
Good Day!

A safer and more explicit style is to combine a here document with the no-operation command :. The : command accepts the here document input and does nothing with it.

</>
Copy
#!/bin/bash

echo "Start"

: <<'COMMENT'
This block is ignored by the script.
It can contain several lines of notes.
The quoted marker prevents variable expansion inside the block.
COMMENT

echo "End"

Output

Start
End

Commenting Out Bash Commands While Testing

During testing, you may want to prevent one or more commands from running. For a single command, add # at the beginning of the line.

</>
Copy
# rm -rf "$target_dir"

For several commands, add # to each line, or use a temporary here document block. When disabling dangerous commands such as rm, mv, or production deployment commands, a separate # on each command line is usually clearer during review.

</>
Copy
#!/bin/bash

# echo "Stopping service"
# systemctl stop my-app
# rm -rf /tmp/my-app-cache
# systemctl start my-app

echo "Dry run complete"

Good Bash Comment Practices in Shell Scripts

Good Bash comments explain why a command is needed, not only what the command already says. A comment such as # delete file above an rm command is usually less useful than a comment explaining why that file is safe to delete.

  • Use comments to explain non-obvious decisions, assumptions, and safety checks.
  • Keep comments close to the related command so they do not become outdated.
  • Do not place secrets, passwords, tokens, or private server details in comments.
  • Prefer clear variable and function names so that fewer comments are needed.
  • Update comments when you change the command below them.

Common Bash Comment Mistakes

Some Bash comment errors happen because # is only treated as a comment marker in the right context.

</>
Copy
#!/bin/bash

name="TutorialKart # Bash"  # the first # is text, the second # starts a comment
path=/tmp/example\#1       # escaped # is part of the value

# This is a real comment
echo "$name"
echo "$path"

Also avoid putting a regular comment before the shebang line. If a script is meant to be run directly, keep #!/bin/bash as the first line of the file.

Bash Comments Editorial QA Checklist

  • Does the tutorial clearly distinguish a normal Bash comment from the shebang line?
  • Does every new Bash example use # comments only where Bash will actually ignore the text?
  • Are multiline comment examples described as here document workarounds, not as native Bash block comments?
  • Do output blocks show only terminal output and not extra commands?
  • Are safety-related examples careful with destructive commands such as rm?

FAQs on Bash Comments

How do I comment one line in a Bash script?

Start the line with #. Bash ignores that line when the script runs, except when the line is the shebang line at the top, such as #!/bin/bash.

Can I write an inline comment after a Bash command?

Yes. Write the command first, add a space, and then add # followed by the comment. Bash runs the command and ignores the text after #.

Does Bash have real multiline comments?

Bash does not have a native multiline comment syntax. A here document with : is commonly used when you need to ignore several lines together.

Why is # printed instead of becoming a Bash comment?

If # is inside quotes or escaped with a backslash, Bash treats it as normal text. It starts a comment only when it appears in a comment position outside quotes.

Should I use comments to disable Bash commands?

Yes, for temporary testing you can add # before a command to stop it from running. For important or risky scripts, remove temporary commented-out code once testing is complete so the script remains easy to review.

Summary of Comments in Bash Scripting

In this Bash Tutorial, we have learned how to write single line comments with #, inline comments after commands, and multiline comment blocks using here documents. We also covered quoted hash symbols, temporary command disabling, and practical rules for writing clear Bash comments.