A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/comments-in-shell-script/ below:

Comments in Shell Script - GeeksforGeeks

Comments in Shell Script

Last Updated : 19 Apr, 2025

Comments are the useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. Comments are usually helpful to someone maintaining or enhancing your code when you are no longer around to answer questions about it. These are often cited as a useful programming convention that do not take part in the output of the program but improve the readability of the whole program.

There are two types of comments:

A single-line comment starts with a hashtag symbol with no white spaces (#) and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment.

The shell script is commented out prefixing # character for single-line comment. Single-line comments are useful for brief explanations of code snippets or specific lines.

Syntax:
# This is a comment
Example:
# Single line comment
echo "hello world"

Output:

In this example, the comment explains the purpose of the script, and it is not executed by the shell.

Multi-line comment is a piece of text enclosed in a delimiter ('') on each end of the comment. Again there should be no white space between delimiter (' '). They are useful when the comment text does not fit into one line; therefore need to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment.

Syntax:
: '
This is a
Multi-line comments
'

Example:

echo "multiline comments"
: ' This section explains the upcoming code block'

Output:

The multi-line comment section is ignored by the shell, making it a perfect place to add detailed explanations or documentation.

Conclusion

Comments are an invaluable part of writing clear, maintainable shell scripts. They help explain the code's logic and intent, making it easier for others (or yourself) to understand and maintain in the future. Using single-line comments for brief notes and multi-line comments for more detailed explanations can make your scripts more readable and professional.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4