[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 14, 2023

How to write comments in bash scripts.

How To Write Comments In Bash Scripts.

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

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. Do not explain something which is simple and obvious to the reader.

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

 

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 on the script starts with the #! characters. This sequence of characters is called Shebang and is used to tell the operating system which interpreter to use to parse the rest of the file.

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

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

Copy

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

Copy

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.

Copy

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

Copy

Using HereDoc is a hack, not a real builtin 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.

Keep reading
How to use clear command in Linux.
September 14, 2023

How to use clear command in Linux.

How To Use Clear Command. In Linux, clear is built-in command which is used to clear terminal screen. This command runs on different distributions of Linux based operating systems like Ubuntu, Fedora, Debian, Kali, Red Hat etc. In this tutorial we will discuss syntax for command echo with examples.

Read article
How to use host command in Linux.
September 14, 2023

How to use host command in Linux.

How To Use Host Command. Follow this article’s guideline to learn how to use hostname command on Linux. Host is a command line utility that makes it possible for the Linux users to find information about a given host on a network. You can use the host command to lookup the domain’s IP address and…

Read article
How to use od command in Linux.
September 14, 2023

How to use od command in Linux.

How To Use Od Command. The “od” is an efficient command that helps to understand the complex data which is not human-readable. In Linux operating system, “od” is a useful command that converts the input in multiple formats with octal format by-default. As we all know, Octal has a base-8 number…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00