shell 脚本中的注释详解

单行注释:

  单行注释就比较简单了,直接在行最前端加上符号 # 即可。具体用法如下所示:

# this is comment test
echo "this is comment test"

  运行结果:

➜  comment git:(master) ✗ sh comment.sh
this is comment test

多行注释:

  多行注释有很多方法,这里就列举几个常用的

1 eof截止符

  eof截止符不但可以用作后续输入命令,还可以用作注释,常用用法:开始注释部分:

输入:<<eof

结束部分:eof

  具体示例如下所示:

 # echo is test
 echo "test"
 echo "test"
 echo "test"
 echo "test"
 echo "test"
 :<<eof
 echo "comment"
 echo "comment"
 echo "comment"
 echo "comment"
 echo "comment"
 echo "comment"
 eof

  运行结果:

➜  comment git:(master) ✗ bash comment.sh
test
test
test
test
test

2 感叹号

  !号一般作为嵌入内容部分,可以用作注释,常用用法:开始注释部分:

输入:<<!

结束部分:

  具体示例如下所示:

 # echo is test
 echo "test"
 echo "test"
 echo "test"
 echo "test"
 echo "test"
 :<<!
 echo "comment"
 echo "comment"
 echo "comment"
 echo "comment"
 echo "comment"
 echo "comment"
 !

  运行结果:

➜  comment git:(master) ✗ bash comment.sh
test
test
test
test
test

3 逗号

  逗号一般作区分内容,也可以用作注释,常用用法:开始注释部分:

输入: '  (注意,逗号和冒号之间要加空格)

结束部分:'

  具体示例如下所示:

# echo is test
echo "test"
echo "test"
echo "test"
echo "test"
echo "test"
: '
echo "comment"
echo "comment"
echo "comment"
echo "comment"
echo "comment"
echo "comment"
'

  运行结果:

➜  comment git:(master) ✗ bash comment.sh
test
test
test
test
test

  自我感觉,这次写的比较清晰了,希望对你有用。

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值