Bash 单行注释与多行注释
1. Bash 单行注释
行开头可以使用 #
注释。
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash
# echo "Hello World!"
echo "Yongqiang Cheng!"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$
#
后面的部分是注释。
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash
# echo "Hello World!"
echo "Yongqiang Cheng!" # echo "Hello World!"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$
#
表示注释,可以放在行首,也可以放在行尾。
2. Bash 多行注释
- 在
<< BLOCK
和BLOCK
之间的内容会被当成注释。
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash
<< BLOCK
echo "Hello World!"
echo "Hello World!"
echo "Hello World!"
BLOCK
echo "Yongqiang Cheng!"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$
- 在
: '
和'
之间的内容会被当成注释,注意:
和'
之间有空格隔开
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash
: '
echo "Hello World!"
echo "Hello World!"
echo "Hello World!"
'
echo "Yongqiang Cheng!"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$
- 在
:<<!
和!
之间的内容会被当成注释
(base) yongqiang@yongqiang:~/bash_work$ cat ./bash_script.sh
#!/bin/bash
:<<!
echo "Hello World!"
echo "Hello World!"
echo "Hello World!"
!
echo "Yongqiang Cheng!"
(base) yongqiang@yongqiang:~/bash_work$
(base) yongqiang@yongqiang:~/bash_work$ ./bash_script.sh
Yongqiang Cheng!
(base) yongqiang@yongqiang:~/bash_work$
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] Bash 教程, https://www.w3cschool.cn/bashshell/
[3] Bash 脚本教程, https://wangdoc.com/bash/index.html