Multiple Line Comments in Bash [With Shortcut Keys]

Commenting in Bash is a practice of including important notes or brief clarifications in a script that are overlooked by the Bash interpreter while executing the script. In addition, multiline commenting refers to commenting out multiple lines simultaneously by using a particular syntax.

You can use the following 3 methods to comment out multiple lines in Bash:

  1. Using the hash # symbol before every line:

#!/bin/bash

echo "printable line"
# echo "comment1"
# echo "comment2"
# echo "comment3"
  1. Using null : command with heredoc notation:

#!/bin/bash

echo "printable line"
: <<comment
echo "comment1"
echo "comment2"
comment
  1. Using : ' comment ' syntax:

#!/bin/bash

echo "printable line"
: '
echo "comment1"
echo "comment2"
'

The following section will show you how to comment out multiple lines using different keystrokes in the three common text editors Nano, Vim, and Emacs.

Multiline Comments in Nano

In the Nano text editor, you can comment out multiple lines by appending a hash # symbol at the start of each line.

The following image indicates a Bash script that is opened in the Nano text editor:

A script opened in Nano text editor

You can comment out multiple lines in the above script using the following steps:

  1. First, move the cursor to the start of the block of code you want to comment.

  2. To set the mark, press ALT + A and move the ARROW keys to the end block to select all the lines.

  3. Now, press ALT + 3 or ESC + 3 to comment out the selected lines with the # symbol.

  4. To unset the mark, click on CTRL + 6.

  5. Lastly, click on CTRL + S to save the file and CTRL + X to exit the Nano editor.

After following the steps above, you will find the script with some parts commented on starting with the # symbol as follows:

Multiple line comments using Nano text editor

Hereafter, when executing the script, the output will be something like this:

Displaying the uncommented lines using Nano editor

From the above image, you can see that the two lines commented out with the # symbol are not executed.

Multiline Comments in Vim

The Vim text editor is packaged with a lot of shortcut functionalities. Using this editor you can comment out multiple lines in a Bash script by following some shortcut steps. Let’s open a script with Vim as follows:

A script opened in Vim text editor

To comment out multiple lines in the above script follow the steps below:

  1. Move the cursor to the first character of the line from where you want to start commenting out.

  2. Then, press CTRL + V to enter the blockwise visual mode.

  3. Now, move the cursor up or down to select the lines you want to comment out.

  4. Enter SHIFT + I to enter the insert mode at the start of the selected lines.

  5. Now, type # to insert this commenting symbol at the beginning of the lines.

  6. After that, press ESC to apply the changes and comment out all the selected lines.

  7. Finally, you are in the normal mode. So, type :w and press ENTER to save the file, and type :q and press ENTER to exit the Vim editor.

Now, you will find the script with some commented lines with the # symbol as follows:

Multiple line comments using Vim text editor

After running the script, you will see an output like the following image:

Displaying the uncommented line using Vim editor

In the image, you can see that the script executes only the uncommented line implying that the lines started with the # symbol are treated as comments and the script does not execute them.

Multiline Comments in Emacs

In the Emacs text editor, you can use the short keys to comment out multiple lines simultaneously in a Bash script. To do so, open a script in the Emacs text editor like the following image:

A script opened in Emacs text editor

Follow the steps below to comment out multiple lines in the above script:

  1. To comment out a block of text, first place the cursor at the beginning of a line.

  2. Now, press CTRL + SPACE to set a selection mark.

  3. Then, move the cursor to the end of the lines you want to comment.

  4. After that, press ALT + ; to comment out the selected block.

  5. At last, press CTRL + X, CTRL + S to save the file, and press CTRL +X, CTRL + C to exit the Emacs editor.

As a consequence of the steps above, you will see some commented parts starting with the # symbol within your script like this:

Multiple line comments using Emacs text editor

When you run the script, you will get to see an output image like below:

Displaying the uncommented line using Emacs editor

This image states that the first three lines with the # symbol are addressed as multiple-line comments and are not executed by the script.

Conclusion

To conclude, multiple-line comments serve as an important annotation for a Bash script. These greatly improve the code readability and make the script easier for future use.

People Also Ask

Does Bash have any default syntax for multiple-line comments?

Actually, it depends. Which procedure you’re going to follow for multiple-line commenting in Bash will decide whether you need to use any particular syntax or not.

Can multiple line comments be nested up in Bash?

No, multiple-line comments cannot be nested up in bash. If you try this, it’ll cause a syntax error.

Do multiple-line comments affect the Bash script’s performance?

No, multiple-line comments don’t affect the Bash script’s performance as they are passive codes and are used only for the documentation of a script.

Can I write multiple-line comments in a conditional statement in Bash?

No, you cannot write multiple-line comments within a conditional statement directly. But you can comment out the entire conditional block using multiple-line comments.

Are single-line comments easier to use than multiple-line comments in Bash script?

Multiple-line commenting in Bash script is not too difficult to append. But comparatively, single-line comments are easier to use script than multiple-line comments because of their simple syntax.

Related Articles


<< Go Back to Bash Comments | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment