Writing Single-line, Inline, and Multi-line Comments in Linux Shell Script

Linux TLDR
Last Updated:
Reading time: 2 minutes

Like other programming languages, bash provides you with the option to ignore a single line, inline, or a block of lines from execution.

The comments always received the least attention due to their simplicity but played a very important role while being used in a program or script.

You can use them to describe the block of lines in your script that can help you understand what you did in the past; others can also benefit from this. Or, you can use the comments to skip the block of lines while debugging your script.

In this article, you will learn three ways to comment lines in your shell script.

  • Single-Line Comment
  • Inline Comment
  • Multi-Line Comment

So, let’s start with the

Single-line Comments in Shell Script

You can use the hash/pound sign (“#“) at the beginning of any line that will be treated as a comment and will be skipped from the execution.

The following is a sample script with two commented lines:

#!/bin/bash

#Initializing Variables
var1=6
var2=5

msg="Your total is"

#Calling Variables
eval "echo $msg" $((var1+var2))

I think you have noticed it until now; if you skipped it, check the first line of this script, where you will find this line: “#! /bin/bash“.

If you’re wondering what this is and whether it will also be skipped, you should remember that this is the line, also known as the “shebang“, in shell script used to determine which shell will be used to run the script.

It is the only line with a special case followed by (“#!“) that is not considered a comment.

Inline Comments in Shell Script

Instead of specifying the comment above or below the line, you can specify it within the same line at the end of the code, followed by (“#“) at the beginning.

The following is the sample script with one inline comment.

#!/bin/bash

var1=6
var2=5

msg="Your total is"  #Declaring the message

eval "echo $msg" $((var1+var2))

I’ve removed the previously added comments from the above script to avoid confusion.

Multi-line Comments in Shell Script

You cannot directly add multi-line or block comments in the shell script. However, there are two ways to do this, first by adding the (“#“) at the beginning of each line you want to skip, but this approach might be inconvenient at the time of adding the comments and especially while removing them.

The second is by using the here document, which I personally find more convenient. In this approach, you will use the undefined redirection to skip the multi-line or block of lines from the shell script.

Note: It is only recommended to use “here document” for debugging purposes because it is not part of the shell’s built-in functionality.

#!/bin/bash

<<Block_comment
var1=6
var2=5
Block_comment

msg="Your total is"

eval "echo $msg" $((var1+var2))

The above script will only output the “Your total is 0” line.

The rest of the code between “<<Block_comment” and “Block_comment” will be skipped during execution.

That is all you need to know about the comments in the shell script.

If you have questions or confusion related to this topic, don’t hesitate to comment on it.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.