Skip to main content
A Bash comment is a lexical construct that instructs the shell interpreter to ignore specific text during the parsing and execution phases. A comment is initiated by an unquoted hash character (#) appearing at the beginning of a word, and it terminates unconditionally at the next newline character (\n). Backslashes inside a comment do not escape the newline; they are treated as literal characters. When the Bash parser encounters a # at the start of a line or preceded by unquoted whitespace or metacharacters, it treats that character and all subsequent characters on the same physical line as a single token to be discarded before command execution.

Syntax and Scope

Comments can be placed at the beginning of a line or inline following a command.

Lexical Exceptions

The # symbol loses its comment-initiating property and is treated as a literal character if it is enclosed in single quotes ('), double quotes ("), escaped with a backslash (\), or if it appears anywhere other than the beginning of a word.

Multi-line Comments

Bash does not possess a native block-comment operator (such as /* ... */ found in C-family languages). To span comments across multiple lines, the standard approach is to prefix every line with the # character.
To bypass prefixing every line, developers often simulate block comments by combining the null utility (:), which does nothing and returns a 0 exit status, with a Here Document (<<).

The Shebang (#!) Directive

If the # character is immediately followed by an exclamation mark (!) at byte zero (the absolute beginning) of a script file, it forms a magic number (0x23 0x21) known as a shebang. While syntactically resembling a comment to the Bash interpreter, the shebang is a directive read by the operating system’s program loader to determine which interpreter to use for executing the file.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More