What Are Single-line Comments in Bash? [2 Cases With Examples]

Single-line comments in a Bash script are the important annotation that makes the script more readable. You can use a single-line comment to add an explanation of codes or disable some particular codes for a while. So, let me turn your attention to this article which will provide a detailed interpretation of single-line comments in Bash to you.

Key Takeaways

  • Learning about single-line comments in Bash.
  • Getting ideas about some practical executions of single-line comments in Bash script.

Free Downloads

What Are Single Line Comments in Bash?

Single-line comments refer to the comments where the line starts with a hash symbol (#) and are placed before or after the code in the Bash script. These comments are kind of short and concise. Moreover, single-line comments are only for quoting a simple brief of code lines and are not executed by the interpreter or compiler.

2 Cases of Single Line Comments in Bash Scripting

Single-line comments are the less explanatory Bash comments. These are related to two points: i) Bash hashing, and ii) Inline comments. A little introduction to these two concerns is given below:

Case 1: Bash Hashing in Case of Single Line Comments

In Bash, hash (#) is the symbol for commenting out a line. A single-line comment prefixed with the hash symbol (#) adds an explanatory text without affecting the execution of codes within the Bash script. Following is an example of bash hashing in case of single-line comments with step by step procedure:

For getting more ideas about bash hashing, visit Bash the Hash.

Steps to Follow >

  1. Open your Ubuntu Terminal.
  2. To open a script in the nano text editor, write the command below:
    nano hash-single.sh
    EXPLANATION
    • nano: A text editor.
    • hash-single.sh: This is a script that you can name by any of your choices. Here, I have named the script by ‘hash-single.sh’.
    Opening a script in Nano text editor
  3. Hereafter, write the following script inside the editor:Script (hash-single.sh) >
    #!/bin/bash
    
    # This is a single-line comment
    # This is another single-line comment
    # This is one more single-line comment
    EXPLANATION

    Here, #!/bin/bash: ‘#!’ is called “Shebang” which is used for executing the bash script. The three lines ‘# This is a single-line comment’, ‘# This is another single-line comment’, and ‘# This is one more single-line comment’ dictate single-line comments as there is the hash symbol before each of these lines.

    Bash hashing in case of single-line commentsFrom the above image, you can see three single-line comments which are prefixed with the hash symbol.

Case 2: Inline Comments in Case of Single Line Comments

The single-line comments that are used at the end of the coded line are specifically called inline comments. These are used at the same line of the code starting with a hash sign without moving to the next line. Below is a simple example of inline comments in case of single-line comments:

Script (single-line.sh) >

#!/bin/bash

number=20
echo "The number is: $number"  #Display the current number
number=$((number+5))  #Increment the ‘number’ variable
echo "The updated number is: $number"  #Display the updated number
EXPLANATION

Here, the comment ‘#Display the current number’ indicates printing the present number. Then,  ‘#Increment the ‘number’ variable’  tells to increase the value of the current number by 5. Finally,  ‘#Display the updated number’ displays the updated number that is been increased.

Inline comments in case of single-line commentsYou can see the single-line comments as inline comments in the above image.

3 Practical Examples of Single-line Comments in Bash

Single-line comments can be of help in various practical schemes. Now, I am going to mention some real clarifications of single-line comments in the Bash script in the following section:

Example 1: Bash Single Line Comments for Troubleshooting

For any troubleshooting purpose within Bash scripts, debugging with single-line comments is quite helpful. Below is an example of single-line comments aiding in debugging:

Script (troubleshoot.sh) >

#!/bin/bash

#Calculating the sum of numbers from 5 to a given number

#Prompt user for entering an input
read -p "Enter a positive integer:" number

#Check if the input is a positive integer
if [[ ! "$number" =~ ^[1-9][0-9]*$ ]]; then
    echo "Invalid input. Please enter a positive integer."
    exit 1
fi

#Initialize the sum variable
sum=0

#Calculate the sum using a loop
for (( i=1; i<=number; i++ )); do
    sum=$((sum + i))  #Add present value of i to the sum
    echo "Current Sum: $sum"  #Debugging: print current sum for each iteration
done

#Display the output
echo "The sum of numbers from 5 to $number is: $sum"
EXPLANATION

Here, the initial comment ‘#Calculating the sum of numbers from 5 to a given number’ dictates the purpose of the script telling what is going to perform. Next, the comment ‘#Prompt user for entering an input’ informs the user that now they have to enter a positive integer. The single-line comment, ‘#Check if the input is a positive integer’  inside the if statement tells to ensure that the given number is a positive integer; if not there will be an error message.

Now, the comment ‘#Initialize the sum variable’ informs users of the initialization of the sum operation. Then, ‘#Calculate the sum using a loop’ states that a for loop will be running to find out the sum. After that, ‘#Add present value of i to the sum’ indicates the addition of the present value of ‘i’ and the sum variable for each iteration. Hereafter, ‘#Debugging: print current sum for each iteration’ describes the debugging purpose by printing the current value of the sum for each iteration. Finally, Stating by the comment ‘#Display the output’, the output format is displayed.

Bash single-line comment for debuggingYou can observe the step-by-step results and identify the unexpected bugs within your codes using single-line comments like the snapshot above.

Example 2: Bash Single Line Comments for Educational Purposes

You can use single-line comments for educational purposes too. These can serve as great documented resources for any kind of tutorial. Here is an easy implementation of single-line comments on finding the factorial of a given number:

Script (factorial.sh) >

#!/bin/bash

#Calculating the factorial of a given number

#Prompt user for entering an input
read -p "Enter a positive integer: " number

#Check if the input is a positive integer
if [[ ! "$number" =~ ^[1-9][0-9]*$ ]]; then
    echo "Invalid input. Please enter a positive integer."
    exit 1
fi

#Initialize the factorial variable
factorial=1

#Calculate factorial using a loop
for (( i=1; i<=number; i++ )); do
    factorial=$((factorial * i))  #Multiply factorial with current value of i
done

#Display the output
echo "The factorial of $number is: $factorial"
EXPLANATION

Here, the initial comment, ‘#Calculating the factorial of a given number’ indicates the overview of the script telling what is going to perform. Next, the comment ‘#Prompt user for entering an input’ informs the user that now they have to enter a positive integer. Then, the single-line comment ‘#Check if the input is a positive integer’ inside the if statement tells to ensure that the given number is a positive integer; if not there will be an error message.

Now, the comment ‘#Initialize the factorial variable’ informs users of the initialization of the factorial operation. Then, ‘#Calculate factorial using a loop’ states that a for loop will be running to find out the factorial. After that, ‘#Multiply factorial with current value of i’ explains to multiply the factorial variable with the present value of ‘i’ for each iteration. Finally, framing by the comment ‘#Display the output’, the output format is displayed.

Bash single-line comment for educational purposeFrom the image, you can see that the single-line comments have turned the program quite easy explaining each section of the code.

Example 3: Bash Single Line Comments for Security Purposes

Single-line comments emphasize the security of a Bash script. Here’s an example of single-line comments on restricting unauthorized access to the Bash script:

Script (security.sh) >

#!/bin/bash

#IMPORTANT: Confirm that only authorized users can access the script.
#This script performs sensitive operations & must be protected.

#Check if the user is authorized
if [[ "$user" != "admin" ]]; then
    echo "Error! You cannot run this script."
    exit 1
fi
EXPLANATION

Here, the initial comment, ‘#IMPORTANT: Confirm that only authorized users can access the script.’ explains to ensure only authorized users to run the script. Then, the comment, ‘#This script performs sensitive operations & must be protected.’ defines that the script needs protection from unusual access as it contains sensitive data. Lastly, ‘#Check if the user is authorized’ specifies verifying the user’s authority while running the script using a conditional statement.

Bash commenting for security purposeThe single-line comments underlined in the above image help to define the unusual users easily.

Conclusion

To summarize, single-line comments as an essential feature serve to clarify codes or programming within a Bash script. So, count on this guide and make a practice of commenting out your code concisely.

People Also Ask

Can I use single-line comments to disable specific lines of code temporarily?
Yes, by adding a hash symbol(#) before a line, you can use single-line comments to disable specific lines of code temporarily and prevent them from execution.

Do single-line comments have any impact on the runtime of a Bash script?
No, single-line comments don’t affect the performance or runtime of a Bash script.

Can I use single-line comments in a conditional statement in Bash?
Yes, you can use single-line comments in a conditional statement or wherever you want in Bash.

Can comments span more than one line?
Of course! You can comment either single-line or multiple lines out.
Are inline comments hidden?
Generally, inline comments are not hidden. Though inline comments are not interpreted by the compiler, they are visible to many developers sometimes.
Does Bash interpreter execute inline comments?
No, inline comments are used only for code explanation. The Bash interpreter does not execute inline comments.
Can I remove inline comments from Bash script?
Yes, you can remove inline comments from Bash script.

Can I disable comments without removing it?
As comments are non-executable text, you cannot disable them without removing from the Bash script.

Related Articles


<< Go Back to Bash Comments | Bash Scripting Tutorial

Rate this post
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