LAST UPDATED: FEBRUARY 1, 2022
Writing Comments In Bash Scripts
In this tutorial, bash Comments we will learn how to give single-line and multiple-line comments in a Bash Script file.
Comments are strings that aid in the readability of a coding program. Comments are disregarded when running the commands in a Bash script file.
When developing Bash scripts keep your code tidy and easily comprehensible. Organizing code in blocks, indenting, providing variable and function descriptive names are numerous approaches to achieve this.
Another technique to increase the readability of the code is by utilizing comments. A remark is a human-understandable explanation that is written in the shell script.
Adding comments to Bash scripts will save a lot of time and effort when you look at your code.
Writing Comment in Bash
Bash ignores anything typed on the line following the hash symbol (#). The sole exception to this rule is the first line in the script starts with #! Character.
Syntax:
# This is a Bash comment.
echo "Code Here" # This is an inline Bash comment.
The blank space following the hash mark is not essential, but it will enhance the comment’s readability.
#!/bin/bash
# this is a one line comment
echo Learn Bash Scripting
a=4
b=4
# addition : here is another line remark
c=$(($a + $b))
# echo result to console
echo $a + $b = $c
~$ ./bash-single-line-comments-example
Learn Bash Scripting
4 + 4 Equals 8
Multiline Comments
To write multiline comments in a bash script, surrounding the comments between <<COMMENT and COMMENT. For example:
#!/bin/bash
# this is a single line comment in bash
echo Learn Bash Script
<<COMMENT
This is a several line remark
In Bash Script
COMMENT
echo Have Good Day!
<<COMMENT
This is a several line remark
End of the provided example
COMMENT
Output:
~$ ./bash-multiple-line-comments-example
Learn Bash Scripting
Good Day!
Conclusion
In this Bash Tutorial, we have learnt how to construct a single-line comment and multiline comments in the bash script file.