Bash Scripting

Bash Comments Explained

Introduction

Bash comments are an important part of clean and readable Bash scripting. When you write a Bash script, comments help explain what the script does, why a command is used, and how different sections work. Comments are ignored by the Bash interpreter, which means they do not affect the script execution.

If you are learning Bash scripting in Linux, understanding comments is very useful. Whether you are creating automation scripts, cybersecurity tools, backup scripts, or system monitoring scripts, comments make your code easier to understand and maintain.

What Are Bash Comments?

A Bash comment is a note written inside a script for human readers. Bash does not execute comments. In Bash, comments usually start with the # symbol.

Example:

# This is a Bash comment
echo "Hello Linux"

In the above example, Bash ignores the first line and only runs the echo command.

Single-Line Comments In Bash

Single-line comments are the most common type of comments in Bash scripting. They are used to explain one line or one small part of a script.

Create a script:

nano comments-example.sh

Add the following code:

#!/bin/bash

# Print a welcome message
echo "Welcome to Bash scripting"

# Show the current user
whoami

# Show the current working directory
pwd

Save the file and run it:

chmod +x comments-example.sh
./comments-example.sh

The comments will not appear in the output. Only the commands will be executed.

Inline Comments In Bash

You can also write comments on the same line after a command. These are called inline comments.

Example:

#!/bin/bash

echo "Checking system user" # This prints a message
whoami # This shows the current logged-in user

Inline comments are useful for short explanations. However, avoid writing very long inline comments because they can make the script hard to read.

Multi-Line Comments In Bash

Bash does not have a dedicated multi-line comment syntax like some programming languages. However, you can write multiple single-line comments.

Example:

#!/bin/bash

# This script displays basic system information.
# It shows the current user, hostname,
# current directory, and system date.

echo "User: $(whoami)"
echo "Hostname: $(hostname)"
echo "Directory: $(pwd)"
echo "Date: $(date)"

This is the safest and most recommended way to write multi-line comments in Bash.

Using Here Document For Multi-Line Comments

Some users use a here document method for multi-line comments:

: << 'COMMENT'
This is a multi-line comment.
Bash will ignore this block.
You can write notes here.
COMMENT

echo "Script is running"

This works, but beginners should mainly use # comments because they are simple and clear.

Best Practices For Bash Comments

Write comments only where they add value. Do not comment on every simple command. Focus on explaining the purpose of the script, complex logic, important variables, and security-related commands.

Good comment example:

# Check failed SSH login attempts from auth.log
grep "Failed password" /var/log/auth.log

Conclusion

Bash comments are simple but powerful. They help make your Bash scripts readable, organized, and easier to debug. Single-line comments start with #, while multi-line explanations are usually written using multiple # lines.

For beginners, using comments properly is a good habit. It makes your Linux automation scripts easier to understand and improves the quality of your Bash scripting practice.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Install Mono on Ubuntu 18.04: C# Compiler and Runtime Guide

Running programs built for Microsoft's framework on a Linux system is easier than you think. Mono is…

6 hours ago

Install OpenCV on Ubuntu 18.04: Step-by-Step Setup Guide

Computer vision technology powers many modern applications, from image editors to facial scanners. OpenCV (Open Source Computer…

6 hours ago

Install VNC on Ubuntu 18.04: Step-by-Step TigerVNC Setup

A remote desktop interface makes it easy to manage a remote computer. VNC (Virtual Network Computing) is…

7 hours ago

Install Gitea on Ubuntu 18.04: Self-Hosted Git Service Guide

Hosting your own code repositories is a great way to keep your projects private. Gitea is a…

7 hours ago

Install Java on Ubuntu 18.04: OpenJDK 11 and OpenJDK 8

Many modern programs require Java to run. From development tools like Eclipse to search systems…

7 hours ago

Configure a Static IP Address on Ubuntu 18.04: Netplan Guide

Setting a static IP address on your server is a smart move. It ensures your…

1 day ago