Bash教程

Bash 评论

Bash 注释

在本主题中,我们将了解如何在 Bash 脚本文件中插入注释。
注释是任何编程语言的必要部分.它们用于定义任何代码或函数的用法。注释是有助于提高程序可读性的字符串。当我们执行 Bash 脚本文件中的命令时,它们不会被执行。
Bash 脚本支持两种类型的注释,就像其他编程语言一样。
单行注释 多行/多行注释

Bash单行注释

要在bash中写单行注释,我们必须在开头使用hash(#)符号的评论。以下是在命令之间包含单行注释的 Bash 脚本示例:
Bash 脚本
#!/bin/bash
#this is a single-line comment in Bash Script.
echo "Enter your name:"
read $name
echo
#echo output, its also a single line comment
echo "The current user name is $name"
#this is another single line comment
Bash 控制台视图
上述脚本在 Bash 控制台中将如下图所示:
Bash Comments
输出
如果我们输入一个名称为 lidihuo,那么输出将如下所示:
Bash Comments
这里可以明显看出,在命令执行过程中注释被忽略了.

Bash 多行注释

在bash脚本中插入多行注释有两种方式:
我们可以通过将< 我们还可以通过将注释括在(:') 和单引号(') 之间来编写多行注释。
阅读下面的例子,可以帮助你理解多行注释的两种方式:
方法一:
Bash 脚本
#!/bin/bash
<<COMMENTS
    this is the first comment
    this is the second comment
    this is the third comment
COMMENTS
echo "Hello World"
Bash 控制台视图
Bash Comments
输出
Bash Comments
方法二:
Bash 脚本
#!/bin/bash
: '
this is the first comment
this is the second comment
this is the third comment
'
echo "Hello World"
Bash 控制台视图
Bash Comments
输出
Bash Comments

结论

在这个话题,我们讨论了如何在Bash Script文件中插入单行和多行注释。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4