How to write comments in Bash Scripts

Well you have written an awesome Bash script. It works perfectly and perhaps there is no need to add new functionality. Well, maybe not for now, at least! At this stage you are happy with the script. However, after few months you reopen your bash script again to add new feature and you get a headache to figure out on how the script actually works. Hence, you need to spend additional energy and time before you can actually start editing the script in order to add new feature.

Well, at this point you regret that you did not put some comments ( notes ) into the script to remind you of how the bash script is structured. Not only that Bash comments serve as excellent notes for you or anybody else who might work with your script they may be to some extend also used as a basic bash script debugging tool.

In this tutorial you will learn:

  • How to comment on bash command line
  • How to write comment bash scripts
  • How to create multiple line comments

Bash Script Comment Example

Bash Script Comment Example

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distribution
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to write comments in Bash Scripts

The simplest explanation on how to insert a comment into a bash script is to say that any line except the first line prefixed with the # is considered as comment by the bash interpreter. Since the same rule applies also to a bash command you can test this definition by simply prefixing any command you with to run on the bash shell terminal. For example try to execute the following commands:

$ echo "Bash Comment"
$ # echo "Bash Comment"

As you can see the second command has been ignored by the Bash interpreter since it was interpreted as a comment. Commenting bash commands is a great way to save your commands into a bash history without the actual execution.

Next try to create a comment within the actual bash script. All what needs to be done is to prefix each line you wish to comment with #.

Here is an example:


day=$(date +%A)

echo "$greeting back $user! Today is $day, which is the best day of the entire week!"
echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"

Next, try to comment some of the lines:


#day=$(date +%A)

echo "$greeting back $user! Today is $day, which is the best day of the entire week!"
echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"

Depending on you syntax highlighter you might see that the commented lines are now ignored.

Unfortunately bash does not allow to comment entire block. Instead, if you wish to comment block you will need to comment each line one by one. Fortunately many text editors make your effort easier.

For example using Kate editor allows you to comment multiple lines at once by simply highlighting all required line a pressing CTRL+d shortcut. To uncomment simple press CTRL+SHIFT+D.

Yet, another example is a text based editor VIM as shown on the below video:

Comments and Discussions
Linux Forum