Comments
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
#!/bin/bash
# This entire line is a comment and does nothing
echo "Hello" # this trailing part is also a comment
Login to try C/C++/Java/PHP code in the editor
# 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
#!/bin/bash
color="#ff00ff"
echo "The color code is $color"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
- 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). - 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. - Forgetting that the shebang line
#!/bin/bashis technically a comment syntactically, but it has special meaning to the OS loader only when it is the very first line.
- 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 ... COMMENTsince:is a no-op command. - Good comments explain *why* code does something, not just *what* it does (the code already shows the what).
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: