← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 6 of 12

Comments

A comment is a note in your script written for humans that Bash completely ignores when running the file.

Single-Line Comments

Any text from a # character to the end of that line is a comment and is not executed by Bash. Comments can appear on their own line or trail after a real command on the same line.

Example: Single-Line Comments

bash
#!/bin/bash
# This entire line is a comment and does nothing
echo "Hello"  # this trailing part is also a comment

# Inside Quotes Is Not a Comment

When a # appears inside single or double quotes, Bash treats it as an ordinary character, not the start of a comment. This is a frequent source of confusion when a string legitimately needs a hash symbol, like a hex color or a hashtag.

Example: # Inside Quotes Is Not a Comment

bash
#!/bin/bash
color="#ff00ff"
echo "The color code is $color"

Faking a Block Comment

Bash has no real multi-line comment syntax, but a common workaround uses a here-document redirected to the : no-op builtin so none of the lines between the markers are executed. This is a trick, not a first-class language feature, so use it sparingly and prefer normal # lines for clarity.

Example: Faking a Block Comment

bash
#!/bin/bash
: <<'BLOCK_COMMENT'
This entire section
is ignored by Bash
no matter how many lines it spans
BLOCK_COMMENT
echo "Code after the block comment still runs"
Common Mistakes
  1. Believing Bash supports multi-line block comments like /* ... */; it does not -- every comment line needs its own # (there are workarounds using here-documents, but they are not true comments).
  2. Putting a # in the middle of a string and expecting it to still start a comment; inside quotes # is just a literal character, not a comment marker.
  3. Forgetting that the shebang line #!/bin/bash is technically a comment syntactically, but it has special meaning to the OS loader only when it is the very first line.
Chapter Summary
  • Anything from a # to the end of the line is ignored by Bash, except inside quotes or when escaped.
  • There is no native multi-line comment syntax in Bash; each line needs its own #.
  • A common trick for a 'block comment' is a here-document sent to : <<COMMENT ... COMMENT since : is a no-op command.
  • Good comments explain *why* code does something, not just *what* it does (the code already shows the what).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.