This post explains how to write comments in bash shell scripts, with accompanying examples.

Comments are code statements containing user-readable text that the shell skips during execution. Every programming language includes a comments feature, providing a description for lines of code or statements.

Inline comments within the code assist developers in editing and better understanding the code.

The bash scripting language allows you to use the following comment types.

  • Single comments
  • Multi-line comments

The comments useful for humans, code is written for scripting

Single line Comment in bash shell

Single-line comments in shell scripts are denoted by the # symbol at the beginning of each line.

These comments include strings providing information about the corresponding line of code in the shell script.

It is essential to place single-line comments on a separate line for clarity.

For inline comments, use the # symbol at the beginning of the comment. Single-line comments always start with the # symbol.

Syntax:

# Single-line comments

Blank space after the # symbol is not required. Following is a single-line comment example in a shell script.

# Single-line comments.
AGE=45
echo "Age:$AGE" ## printing age inline comment

Multi-Line comments in a shell script

Multi-line comments involve using more than one line for comments.

First way, to creating multi-line comments is by utilizing single-line comments with each line starting with the single-line comment symbol.

Syntax:

# Line1 comments
# Line1 comments
# Line1 comments

The second Way to create a multi-line comment is by enclosing multiple lines within (:) and (').

This syntax involves:

  • Comments starting with a colon (:) followed by '.
  • It followed by multiple lines of comments.
  • Comments concluding with '. Here is the syntax:
: '
multiline comments example1
multiline comments example2
multiline comments example3
'

Here is a Multi-Line Comment example

: '
multiline comments example1
multiline comments example2
multiline comments example3
'
echo "testing multi-line comments"

It is useful for incorporating more text spanning multiple lines, serving documentation purposes as well.

Conclusion

In summary, we have learned how to add both single and multi-line comments in shell script programming.