Cybersecurity Updates & Tools

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.