Bash comments are used to provide descriptions and explanations for the bash script. Bash comments can be used to add description and explanation for a variable, function, code block, or for the complete script. Comments can be used later used to understand the bash script and related code blocks easily and make changes or updates in an easy way.
hash mark (#) is used to create a single-line comment in Linux bash. The hash mark is located at the start of the line and the whole line became a comment which is not executed as a bash comment in the bash interpreter.
# This is a comment echo $user # This is a singleline comment too
inline comment is used to explain the current line. The inline comment starts with the hash sing location to the end of the line for the current line. The bash statement in the current line is executed properly. After the hash sign is not executed as it is interpreted as comment.
echo "linuxtect.com" # This line prints the name of the website
The bash provides multiline or block comments in order to create comment with multiple lines. The bash do not provides unique sign to create mutiline comment which is different from the single line comment. The hash sign can be used to create multiline or block comment by using it multiple times as a block. In the following example we use the hash sign in multiple lines to create multiline or block comment.
#This is a multiline comment #This is a block comment #This is a multiline comment echo $user # This is a singleline comment too
The bash statements can be comment out in order to make them as comment. The comment out operation prevents the execution of the bash statements by the bash interpreter. In the following example we use the echo command which display some text in the standard output.
# This is a test code echo "The execution is arrived"
We comment out the echo statement and it became a comment.
# This is a test code # echo "The execution is arrived"