The purpose of writing comments in a bash script is to provide explanations or additional information about the code. They can be used to leave notes for the programmer about how the code works, which can be helpful when the code is being modified or updated.
Comments are not executed as part of the program, so they are useful for providing information to others who are reading the code. This article will describe the importance of writing comments in bash scripts with the below timeline:
- How to Write Comments in Bash Script?
- Single Line Comment in Bash Script
- Inline Comment in Bash Script
- Multi-Line Comment in Bash Script
- Using Several Single-Line Comments
- Using “<<” Format
- Using “: ‘” Format
Let’s discuss these methods in detail.
How to Write Comments in Bash Script?
Several ways to write a comment in a bash script will be discussed in this section.
Single Line Comment in Bash Script
As the name indicates, the single line comment would comment one line, which will not be executed. The single-line comment starts with the pound (#) symbol, and its purpose is to describe or provide a hint about the code below, which helps the programmers to understand the code.
For instance, two single-line (line starting with #) comments in the bash script are shown below:
#!/bin/bash #Display the current working directory pwd #Lists the files in a specific directory ls
Allow the execute permissions to run the bash script by utilizing the below-mentioned command:
$ chmod u+x sample_script.sh
Run the script to check its output using the below-mentioned command:
The output displays the execution result for the “pwd” and ”ls” commands from the bash script.
Inline Comment in Bash Script
The inline comments are used to provide a summary or hint about a specific line. It starts with the pound (#) symbol and does not execute similarly to other comments. The single-line comment usually takes the whole line, while the inline comment is written in the same line as the code.
For instance, to sum two numbers with several inline comments to provide the summary about that line is written below:
#!/bin/bash echo "Enter First Number: "; read a #Taking first number echo "Enter Second Number: "; read b #Taking second number sum=$(($a + $b)) #adding two numbers echo The sum is: $sum #displaying the sum
To allow the execute permissions to the run the script, utilize the below command:
$ chmod u+x sample_script.sh
Now, run the bash script with the below command:
Multi-Line Comment in Bash Script
Different formats are used to write multi-line comments explained in this section.
Using Multiple Single-Line Comments
The most common method used to perform multi-line comment are using several single-line (#) comments. For instance, to below script is commented on two lines and then prints the code:
#!/bin/bash #First Line Comment #Second Line Comment echo "The logged-in user is: $HOSTNAME"
To run the bash script, execute this command:
The above picture shows the output for the command while the comments are not executed.
Using “<<” Format
The “<<” format can be used to make comments on several lines simultaneously. To start the comment, we use the << “Word” to start the multi-line comment, while the comment ends on the Word as well. The Word can be replacedwith any specific word.
The approach is used when we need to comment on many lines; let’s do it with the below script:
#!/bin/bash << "COMMENT" First Comment Line Second Comment Line Third Comment Line COMMENT echo "The logged-in user is: $HOSTNAME"
To run the above script, utilize this command:
Using “: ‘” Format
Several lines can also be commented using the “: ‘” format. In this format, we use the : ‘ to start and end the comment with the ‘ symbol. Let’s use this format to comment on several lines as shown in the below example:
#!/bin/bash : ' First Comment Line Second Comment Line Third Comment Line' echo "The logged-in user is: $HOSTNAME"
Let’s run the bash script, with this command:
Several commented lines are not executed while the output shows the result for the echo command.
These are all the possible methods to write comments in bash scripts.
The comments in Linux bash scripts provide a summary or hint about the code, which increases the readability and helps the programmers to understand the code. Several methods are used to write comments, such as single-line, inline, and multi-line comments, as described in this tutorial.