Shell Script · Comments

Shell Scripting · Lesson 5

Comments

Use comments to document shell scripts properly. Single line and multi line comment techniques. Writing readable production scripts. Industry standard documentation style.

What is a Comment?

A comment is a line of text that is ignored by the shell. It is used to explain code, add notes, or temporarily disable commands.

Comments improve readability and make scripts easier to maintain.

Why Use Comments?

Clarity

Helps others (and yourself) understand what the script does.

Maintenance

Makes it easier to modify scripts later.

Debugging

You can disable lines temporarily during testing.

Types of Comments

Shell scripting mainly supports single-line comments. Multi-line comments are simulated using special techniques.

Comment Examples

1. Single Line Comment

Use # to add a comment on a single line. The shell ignores everything after #.

terminal — bash
comments
# This is a single-line comment
echo "Hello World"

2. Inline Comment

Comments can also appear after a command on the same line.

terminal — bash
comments
echo "Hello World" # This prints a message

3. Multi-line Comment (Using : << )

Shell does not have real multi-line comments, but this is a common workaround.

terminal — bash
comments
: << 'COMMENT'
This is a multi-line comment.
Nothing inside this block will be executed.
You can write multiple lines here.
COMMENT

4. Commenting Multiple Lines Using #

Each line must start with # for a block-style comment.

terminal — bash
comments
# This is line one
# This is line two
# This is line three

Common Mistakes

Avoid These Mistakes

  • Forgetting # before comments.
  • Adding spaces before # in shebang line.
  • Assuming multi-line comments work like other languages.
  • Leaving debug comments in production scripts.

Practice Task – Comments

  • Create a script with a header comment explaining its purpose.
  • Add inline comments explaining each command.
  • Try using a multi-line comment block.

Next lesson: Variables in Shell Scripting