JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

How to Add Comments in Bash Scripts

Author:JIYIK Last Updated:2025/04/06 Views:

Comments are lines that are ignored by the interpreter and are only used to describe what is happening in the code or to provide insight into a particular block or line of code. Comments make it easier for the reader to understand the code. We can use comments to describe what is happening in the code in human language. It can also be helpful when we review our code after writing it for a while. Comments are also useful when debugging the code. Instead of deleting the part that is suspected to have a bug, we can comment out the specific part and then debug the code.

#We can use and heredocto write comments in Bash . Generally speaking, #is used to write single-line comments and heredocis used to write multi-line comments.

BashSingle-line comments in

We use #the _ notation Bashto write single-line commands in . #After _, #!everything except the first line containing _ is ignored when interpreting the script. This special sequence of _ that appears on the first line #!is called _ Shebang, and it is used to decide which interpreter to use.

Comments can start at the beginning of a line, or even inline with code.

#!/bin/bash
echo "Hello World!" # echo is similar to print statement.
# end of the code

Output:

Hello World!

Here, the first line indicates which compiler to use /bin/bash.

On the second line, we have the inline comment, and on the third line, we have a comment that starts at the line.

BashMulti-line comments in

BashMulti-line comments are not supported. BashOne way to write multi-line comments in is to use single-line comments for each line.

# This is a
# multiline comment in Bash
echo "Hello World!"

Output:

Hello World!

Here, lines 1 and 2 can be considered as multi-line comments, with each line interpreted as a separate comment by the interpreter.

We can also use Heredocto write multi-line commands. Heredocis a way to pass multiple lines of input to a command. If Heredocis not redirected to any command, we can Heredocuse as a multi-line comment.

#!/bin/bash

<< 'Comment'
    Everything inside the
    HereDoc body is
    a multiline comment
Comment
echo "Hello World!" 

Output:

Hello World!

Here, lines 3-7 are treated as multi-line comments by the interpreter.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

MySQL Notes

Publish Date:2025/04/09 Views:79 Category:MySQL

In this article, we will introduce MySQL comments. We will also understand what type of comments should be used and where. Comments are written to describe the code and make it easier to understand. We also use comments to ignore a part of

Move Multiple Files in Linux Bash

Publish Date:2025/04/06 Views:97 Category:OPERATING SYSTEM

In this article, we will explain how to move multiple files to the same directory in Linux. We will explain different methods such as typing multiple file names, using wildcard characters ( * ) to represent similar file names and/or identic

How to Compare Strings in Bash

Publish Date:2025/04/06 Views:190 Category:OPERATING SYSTEM

We can compare strings using various comparison operators and check if a string contains a substring using regular expressions. String comparison in Bash String comparison means checking whether the given strings are identical or not. Two o

tr Command in Linux Bash

Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM

In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma

Differences between Bash Profile and Bashrc

Publish Date:2025/04/05 Views:188 Category:OPERATING SYSTEM

This article explains the difference between ~./bash_profile and files in Bash. ~/.bashrc What are startup files in Bash? Startup files are files that are executed after the shell is started. The startup files depend on the type of shell th

Differences between Sh and Bash

Publish Date:2025/04/05 Views:145 Category:OPERATING SYSTEM

This article explains what a shell is, find out which shell you are currently using, check a list of all available shells, and the difference between sh and . bash What is a Shell A shell is a computer program that accepts commands. It also

How to Append Text to a File Using Bash

Publish Date:2025/04/05 Views:83 Category:OPERATING SYSTEM

We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th

How to Concatenate Strings in Bash

Publish Date:2025/04/05 Views:65 Category:OPERATING SYSTEM

String concatenation is one of the most widely used operations in programming. It refers to connecting two or more strings by putting one string at the end of another string. To concatenate strings in Bash, we can write string variables one

Configure Git Bash with Visual Studio Code

Publish Date:2025/03/31 Views:138 Category:Git

This article outlines the steps to configure Git Bash with Visual Studio Code on Windows. By default, VSCode uses PowerShell as the integrated terminal. We can configure VSCode to use Git Bash as the integrated terminal. Make sure you have

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial