Comments in Shell Script (original) (raw)

Last Updated : 16 Feb, 2026

Comments in shell scripts are used to explain code and improve readability. They are ignored by the shell during execution and do not affect the program output. Comments are mainly used for documentation and code maintenance. They help developers understand the purpose and logic of commands, especially when scripts become long or complex.

To show how comments are used to explain script behavior without affecting execution.

This script displays the current user and working directory

echo "Current User:"
whoami # Displays the username

echo "Current Directory:"
pwd # Prints the present working directory

**Output:

basic_comments_vim

Script using comments

basic_comments_OP

Executing script containing comments

Syntax

comment text

Single-line comments are used to explain a single command or a small block of code. They begin with the # symbol, and everything after it on the same line is ignored by the shell.

**Syntax:

comment text

**Example 1: Commenting a Line

To explain what a specific command does.

**Command:

This command prints a message on the terminal

echo "Hello World"

**Output:

single_line_commentsingle_line_comment_OP

**Example 2: Disabling a Command

To prevent a command from executing without deleting it.

**Command:

echo "This line will not run"

echo "This line will run"

**Output:

disable_commanddisable_command_op

Shell scripting does not support native multi-line comments. However, multi-line comments can be created using commands that do nothing, such as : (null command). This method is useful for adding long explanations or documentation blocks.

**Syntax:

: ' multi-line comment '

**Example: Using : for Multi-line Comments

To include a block of text that spans multiple lines and is ignored during execution.

**Command:

: '
This is a multi-line comment.
The shell ignores everything inside this block.
'
echo "Script executed"

**Output:

multi_line_commentmulti_line_comment_op

Shebang Line (Interpreter Directive)

The shebang line is a special comment placed at the very beginning of a shell script. It indicates the interpreter that should execute the script.

**Format:

#!/bin/bash

**Note: Although it starts with #, it is handled specially by the operating system.

Common Pitfalls and Misconceptions