Bash Comments
Comments are strings that help in the readability of a program. Comments are ignored by shell and are not executed.
Bash provides only single line comments. However, multiline comments are also feasible with a little hack.
In this tutorial, we shall learn how to provide single line and multiple line comments in a Bash Script file.
Bash Single Line Comments
To write single line comments in bash, start the line with the hash symbol (#). HashBang (#!) in the first line of the script file is the only exception. Following is an example Bash Script that has single line comments in between commands.
In the following example, we write single line comments that span the whole line and single line comments after a bash statement.
Bash Script File
#!/bin/bash
# this is a single line comment in bash
echo Learn Bash Scripting
a=2 # this is also a comment, but after the command in same line
b=4
# addition : this is another single line comment
c=$(($a + $b))
# echo result to console
echo $a + $b = $c
Output
~$ ./bash-single-line-comments-example
Learn Bash Scripting
2 + 4 = 6
Bash multiline comments
To write multiline comments in bash scripting, enclosing the comments between <<COMMENT
and COMMENT
. Following is an example of demonstrating multiple line comments or block comments in Bash Scripting.
In the following example, we write multiline comments using <<
.
Bash Script File
#!/bin/bash
# this is a single line comment in bash
echo Learn Bash Scripting
<<COMMENT
This is a multiple line comment
In Bash Scripting
COMMENT
echo Good Day!
<<COMMENT
This is a multiple line comment
End of the example
COMMENT
Output
~$ ./bash-multiple-line-comments-example
Learn Bash Scripting
Good Day!
Conclusion
In this Bash Tutorial, we have learned how to write a single line comment and multiline comments in Bash script file.