Comments in Bash
Comments are an essential part of any programming language. It is used to describes the uses of any code or functions. Similar to the other programming languages bash scripts also support comments. There are two types of comments, single-line comment and multi-line comment.
We have described below both types of comments:
Bash – Single Line Comments
Single line comments are started with a hash (#) symbol. As per the title, it must be finished in a single line. No newline character “\n” should be there. There is no limit to single-line comments in a shell script.
As an example, check the below sample shell script with the single-line comments.
1 2 3 4 5 6 7 | #!/bin/bash # This is a single-line comment echo "Hello World" # This is another single line comment |
Note: The first line (topmost line) of the script also starts with a # symbol. But that is parsed as a shebang character, not as a comment.
Bash – Multiple Line Comment
You can also use multi-line comments in bash scripts. The multi-line commends can be written in two ways.
The below example shell script will show you the first method to define the multi-line comments.
1 2 3 4 5 6 7 8 9 | #!/bin/bash <<COMMENTS This is comment line 1 This is comment line 2 This is comment line 3 COMMENTS echo "Hello World" |
Another way of writing multi-line comments in a shell script.
1 2 3 4 5 6 7 8 9 | #!/bin/bash : ' This is comment line 1 This is comment line 2 This is comment line 3 ' echo "Hello World" |
The first comment started with <<ANYSTRING and ends with same string ANYSTRING. The second multi line comments started with : '
and ended with single quote '
only.