How to add multi-line comments in bash

By default shell script can be commented out by prefixing # character. This need to be done every line as there is no mutiline comment option in bash.

But we can add multiline comment using one simple trick.

This can be acheived by the use of : (colon) which is a shell builtin command that is basically a NOP, a null operation that does nothing except return true.

The syntax is given below.

: '
  comment1
  comment2
'

Note that since it is a command, it can accept arguments and since it ignores its arguments, in most cases it superficially acts like a comment. But give extra care on using variables and special characters in the argument part as its expansion might be dangerous sometimes.

Leave a Reply

Your email address will not be published. Required fields are marked *