Writing Comments in Bash Scripts

Updated on

3 min read

Bash Script Comments

When writing Bash scripts, it is always a good practice to make your code clean and easily understandable. You can achieve this by organizing code into blocks, using indentation, and giving variables and functions descriptive names.

Another way to improve the readability of your code is to use comments. A comment is a human-readable explanation or annotation written within the shell script.

Adding comments to your Bash scripts will save you a lot of time and effort when you look at your code in the future. For instance, if you want to modify a script you wrote months or years ago, you may not remember why you wrote a particular piece of code unless you added a comment. Therefore, adding comments to your code can help you understand it better and make future modifications hassle-free.

The comments also help other developers and system administrators who may need to maintain the script to understand your code and its purpose.

Comments are used to explain the code. For example, if you have a complex regex or parameter substitution inside your Bash script, you should write a comment describing what the code does. Comments should be short and to the point. Refrain from explaining something that is already clear and straightforward to the reader.

In this article, we’ll cover the basics of writing comments in Bash.

Writing Comments in Bash

Bash ignores everything written on the line after the hash mark (#). The only exception to this rule is when the first line of the script starts with the #! characters. This sequence of characters is called Shebang and tells the operating system which interpreter to use to parse the rest of the file.

Comments can be added at the beginning of the line or inline with other code:

# This is a Bash comment.
echo "This is Code" # This is an inline Bash comment.

The blank space after the hash mark is not mandatory, but it will improve the comment’s readability.

If your text editor supports syntax highlighting, comments are usually represented in green.

Comments are also useful when testing a script. Instead of deleting some lines or blocks, you can comment them out:

# if [[ $VAR -gt 10 ]]; then
#  echo "Variable is greater than 10."
# fi

Multiline Comments in Bash

Unlike most of the programming languages, Bash doesn’t support multiline comments.

The simplest way to write multiline comments in Bash is to add single comments one after another:

# This is the first line.
# This is the second line.

Another option is to use HereDoc . It is a type of redirection that allows you to pass multiple lines of input to a command. If the HereDoc block is not redirected to a command, it can serve as a multiline comments placeholder:

<< 'MULTILINE-COMMENT'
    Everything inside the
    HereDoc body is
    a multiline comment
MULTILINE-COMMENT

Using HereDoc is a hack, not a real built-in way to write multiline Bash comments. To avoid any issues, you should prefer using single-line comments.

Conclusion

Writing comments is a good practice and helps other developers, including future self, to understand the shell script. In Bash, everything after the hash mark (#) and until the end of the line is considered to be a comment.

If you have any questions or feedback, feel free to leave a comment.