如何在 Bash 脚本中添加注释
Bash 注释 现场文档
![](/data/attachment/album/202310/19/113658dld7b9987xa89l99.jpg)



> 
> 你有要说明的地方吗？请将它们添加到你的 Bash 脚本中。
> 
> 
> 


在 Bash 脚本中添加注释是保持代码整洁且易于理解的最有效方法之一。


你可能会问为什么。


假设你的脚本包含一个复杂的正则表达式或多个复杂的代码块，在这种情况下，你可以添加注释，以便其他开发人员或你可以了解该代码块的含义。


注释掉部分代码也有助于调试脚本。


在本教程中，我将引导你了解在 Bash 脚本中添加注释的三种方法：


* 单行注释
* 行内评论
* 多行注释


那么让我们从第一个开始。


### Bash 中的单行注释


要添加单行注释，你必须将哈希符号（`#`）放在行的开头并编写注释。


这是一个简单的例子：



```
#!/bin/bash
# This is a comment
echo "Hello, World!"

```

执行时，注释将被忽略，当我执行上面的命令时，它看起来像这样：


![](/data/attachment/album/202310/19/113726gw754x4v79jws4al.png)



> 
> ? `#` 注释规则的唯一例外是脚本开头的 `#!/bin/bash` 行。它称为 [释伴](/article-3664-1.html)，用于指定运行脚本时要使用的解释器。有不同的 Shell，语法也可能不同。因此，一个好的做法是指定脚本是为哪个 Shell 编写的。例如，如果是 ksh，则可以使用 `#!/bin/ksh`.
> 
> 
> 


### Bash 脚本中的内联注释


或者，你可以将注释放在代码块内以记录该特定行的用途。



> 
> ? `#` 之后的任何内容在该行结束之前都不会执行，因此请确保在代码末尾添加注释。
> 
> 
> 


这是一个简单的例子：



```
#!/bin/bash
echo "Hello, World!" #Prints hello world

```

![](/data/attachment/album/202310/19/113726w6ig6u896aara8er.png)


### Bash 中的多行注释



> 
> ? Bash 中没有内置的多行注释功能。但是，有一些解决方法可以实现多行注释效果。
> 
> 
> 


顾名思义，Bash 脚本中的多行注释允许你在多行中编写注释，或者通过将它们放在多行注释部分来阻止执行代码块：


1. 在每行的开头使用 `#`
2. [冒号表示法](https://stackoverflow.com/questions/3224878/what-is-the-purpose-of-the-colon-gnu-bash-builtin)（使用冒号后跟单引号）
3. 现场文档（使用 `<<` 后跟分隔符）


那么让我们从第一个开始。


#### 1、对块注释的每一行使用 `#`


如果你的目的是解释脚本的一部分，我建议使用此方法。毕竟，`#` 才是真正的注释功能。


这也是许多开发人员使用的方法。


假设你必须在一开始就解释脚本的用途、作者信息或许可信息。你可以这样写：



```
#!/bin/bash

######################################
## This script is used for scanning ##
## local network                    ##
## Licensed under GPL 2.0           ##
######################################

这个 Bash 脚本的其它部分……


```

当你想说明 Bash 脚本的行为方式时这不错。如果你正在调试 Bash 脚本并希望隐藏部分脚本，则在所需代码的每一行开头添加 `#` 并在调试后将其删除是一项耗时的任务。


接下来的两节将帮助你做到这一点。


#### 2、冒号表示法


要使用冒号表示法，请在 `: '` 和结束的 `'` 之间编写块注释，如下所示：



```
#!/bin/bash

: '
This is how you can use colon notation
And this line too will be ignored
'
echo "GOODBYE"


```

当你执行上面的脚本时，它应该只打印 `GOODBYE`。


![](/data/attachment/album/202310/19/113727fuuqjuvj7vwjjvm7.png)


#### 3、现场文档


到目前为止，这是在 Bash 中编写多行注释的最流行的方法，你可以使用 `<<` 后跟分隔符（一组用于指定注释开始和结束的字符）。


（LCTT 译注：在编程领域，“**here document**” 是一个常见的术语，特指在脚本语言（如 Perl、Bash）中，能够直接在代码内部嵌入并处理一个数据块或文本串的技术。尽管传统上我们将它翻译为“嵌入式文档” 或不翻译，但这个译法似乎并不能完全地体现出原文的感觉和含义。为了让这个概念变得更为直观和易理解，我们建议将 “here document” 翻译为 “**现场文档**”。“现场”相比于“嵌入式”，更好的传达了文档就在代码的当前位置，或代码“现场”的含义。这样的译法也与原文 “here document” 中 “here”（这里）的含义更为契合。我们希望这个译法能够在未来得到更广泛的使用和认可，让编程的世界因语言的精准而变得更美好。PS., 该译法和解释得到了 ChatGPT 的建议和生成。）


使用方法如下：



```
#!/bin/bash

<<DELIMITER
Comment line 1
Comment line 2
DELIMITER

echo "Hello, World!"

```

还困惑吗？ 这是一个简单的例子：



```
#!/bin/bash

<<COMMENT
This is a multi-line comment using a here document.
You can add as many lines as you want between <<COMMENT and the terminating keyword.
This block won't be executed by the shell.
COMMENT

echo "Hello, World!"

```

在上面的示例中，我使用了 `COMMENT` 作为分隔符，但你可以使用任何内容，但要确保它从注释中脱颖而出，否则会造成混乱。


当我执行上面的脚本时，它给出了以下输出：


![](/data/attachment/album/202310/19/113728w887lssls0j0yaml.png)


正如你所看到的，它忽略了从 `<<COMMENT` 到 `COMMENT` 的所有内容。


我希望本指南对你有所帮助。


*（题图：MJ/7c2a3ef4-5256-405e-b757-23e0ba1f0bc5）*




---


via: <https://itsfoss.com/bash-comments/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Adding comments in bash scripts is one of the most effective ways to keep your code clean and understandable.

You may ask why.

Let's suppose your script contains a complex regex or multiple complex blocks of codes and in that case, you can add comments so other developers or you can have an idea of what that block of code was meant to be.

Commenting out part of code also helps in debugging scripts.

In this tutorial, I will walk you through three ways to add comments in bash scripts:

- Single-line comments
- In line comments
- Multi-line comments

So let's start with the first one.

## Single-line comments in bash

To add single-line comments, you have to put the hashtag (#) at the beginning of the line and write a comment.

Here's a simple example:

```
#!/bin/bash
# This is a comment
echo "Hello, World!"
```


While executing, the comments will be ignored and when I executed the above command, it looked like this:

![Use single-line comments in bash](https://itsfoss.com/content/images/2023/10/Use-single-line-comments-in-bash.png)

[shebang](https://linuxhandbook.com/shebang/)and it used to specify the interpreter to be used while running the script. There are different shells and the syntax may differ. So, a good practice is to specify for which shell the script was written. For example, if it were for ksh, you would use #!/bin/ksh

## Inline comments in bash scripts

Alternatively, you can put the comment inside the code block to document what is the purpose of that specific line.

Here's a simple example:

```
#!/bin/bash
echo "Hello, World!" #Prints hello world
```


![How to use the single-line comments in bash](https://itsfoss.com/content/images/2023/10/How-to-use-the-single-line-comments-in-bash.png)

## Multi-line comments in bash

As the name suggests, multi-line comments in bash scripting allow you to write comments in multiple lines or prevent executing block of code by putting them in multiline comment section:

- Use # at the begnning of eacj line
[Colon notation](https://stackoverflow.com/questions/3224878/what-is-the-purpose-of-the-colon-gnu-bash-builtin)(uses colon followed by a single quote)- Here document (uses << followed by delimiter)

So let's start with the first one.

### 1. Use # for each line of block comment

This is what I suggest to use if your purpose is to explain part of the script. After all, # is the actual commenting feature.

And this is what many developers use, too.

Suppose you have to explain the purpose of the script, author info or licensing information in the beginning. You can write it like this:

```
#!/bin/bash
######################################
## This script is used for scanning ##
## local network ##
## Licensed under GPL 2.0 ##
######################################
rest of the bash script code
```


That's fine when you know how your bash script behaves. If you are debugging a bash script and want to hide part of the script, adding # at the beginning of each line of the required code and then removing them after debugging is a time-consuming task.

The next two sections help you with that.

### 2. Colon notation

To use the colon notation, you write block comments between `: '`

and the closing `'`

as shown here:

```
#!/bin/bash
: '
This is how you can use colon notation
And this line too will be ignored
'
echo "GOODBYE"
```


When you execute the above script, it should only print GOODBYE:

![Use multi line comments using colon notation in bash](https://itsfoss.com/content/images/2023/10/Use-multi-line-comments-using-colon-notation-in-bash.png)

### 2. Here document

By far, this is the most popular way to write multiline comments in bash which you use `<<`

followed by a delimiter (a set of characters to specify the start and the end of the comments).

Here's how you use it:

```
#!/bin/bash
<<DELIMITER
Comment line 1
Comment line 2
DELIMITER
echo "Hello, World!"
```


Still confused? Here's a simple example:

```
#!/bin/bash
<<COMMENT
This is a multi-line comment using a here document.
You can add as many lines as you want between <<COMMENT and the terminating keyword.
This block won't be executed by the shell.
COMMENT
echo "Hello, World!"
```


In the above example, I have used `COMMENT`

as a delimiter but you can use anything but make sure it stands out from the comment or it will create confusion.

When I executed the above script, it gave me the following output:

![Use multi-line comments in bash using << and delimeter](https://itsfoss.com/content/images/2023/10/Use-multi-line-comments-in-bash-using----and-delimeter.png)

As you can see, it ignored everything inside <<COMMENT

## Learn bash from scratch!!

If you are planning to learn bash from scratch or want to skim through all the basics, we made a detailed guide for you:

[Learn Bash Scripting For Free With This Tutorial SeriesNew to bash? Start learning bash scripting with this series in am organized manner. Each chapter also includes sample exercises to practice your learning.](https://itsfoss.com/tag/bash-basics/)![](https://itsfoss.com/content/images/2023/07/bash-basics.png)


![](https://itsfoss.com/content/images/2023/07/bash-basics.png)

I hope you will find this guide helpful.