Comments are used in programming languages as well as in Bash script for writing descriptions. Often you want to describe the actions you have performed or commands you have used. So that it can remind you what and why you have performed this action when you open the script later.
This is similar to taking notes of the steps you take and about the things involved in any process. The description you provide can also be helpful for others in understanding your script, in case you hand over your script to them. This is very useful to explain your script when you are working on a large project or big team.
In this tutorial, you will learn what are the types of comments and how to use them in Bash script along with suitable examples.
Comments in Bash scripts
Commenting is a useful way of describing different elements of your Bash script. It can be the description of the entire script, of an individual element, or any command that you have written.
It is important to mention that comments are a description for human understanding. They are not executed by the shell.
Similar to computer programming languages, you can use two types of comments in a Bash script:
- Single-line comments
- Multi-line comments
These are the comments which are written in one line. To write such comments you have to use the # sign. Normally, you can use single-line comments at the start of a Bash script to describe what this script is all about.
You can write information about several useful attributes of your scripts using this type of comment. Some examples are purpose, version, creation date, author, etc. This is shown in the example that fellows.
#! /bin/bash # Purpose: To demonstrate the use of Comments # Version: 1.0 # Create Date: Feb 11th 2022 # Website: bytexd.com # Author: Muhammad Aziz # Start # echo "Hello World" # End #
Remember that this information is only for humans to read and understand. The shell would not execute these statements as they are commented. You can observe this in the output of the above script.
As you can see only the text written in echo command is displayed as the output, and the rest of the statements are ignored by the Bash shell and hence not displayed.
It is not necessary that a comment will start from the beginning of a line. You can write a comment after writing any command, for example:
echo "Hello World" # this command is used to display text
Here you can see that the comment is written after the echo command, explaining what this command is doing. In this way, you can write comments with any command or after every action so that the other person reading this script can understand.
If you have noticed every Bash script starts with
#! signs. Don’t confuse it with single-line comment sign
#, because this first line is not a comment. The
# sign when followed with an exclamation sign! tells the path of the shell that would process this script. In the case of Bash scripts, the first line would be the path of the Bash shell, as shown below:
As its name implies, these types of comments are written in multiple lines. So, instead of using a
# sign at the start of each line, you need to specify a colon (
:) and a single quote (
') separated by a space, at the start of comments. The combination (
: ') specifies the beginning of multi-line comments, whereas the
' represents its end. This is shown in the example below.
#! /bin/bash : 'Purpose: To demonstrate the use of Comments Version: 1.0 Create Date: Feb 11th 2022 Website: bytexd.com Author: Muhammad Aziz' # Start # echo "Hello World" # End #
Here you can see that instead of writing
# with every line
: ‘ ‘ is used, as the comments are multi-line.
If you find this method difficult, don’t worry. There is another way to write multi-line comments that is to use double forward slashes. However, you have to use double less than sign
<< before forward slashes, give space in between, and write forward slashes on separate lines.
#! /bin/bash << // Purpose: To demonstrate the use of Comments Version: 1.0 Create Date: Feb 11th 2022 Website: bytexd.com Author: Muhammad Aziz // # Start # echo "Hello World" # End #
In this tutorial, you learned how to use single and multi-line comments in Bash scripts. It is worth mentioning here that C-style comments using the delimiters
*/ cannot be used in Bash scripts. Rather
# is used for single-line comments, whereas
: ‘ ‘ or
// // are used for multi-line comments.