shell 腳本中的註釋詳解

  上次寫了shell腳本的註釋,沒想到那麼多人的須要,也存在很多不足。此次作個補充吧。前端

單行註釋:git

  單行註釋就比較簡單了,直接在行最前端加上符號 # 便可。具體用法以下所示:shell

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

  運行結果:bash

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

多行註釋:this

  多行註釋有不少方法,這裏就列舉幾個經常使用的spa

1 eof截止符code

  eof截止符不但能夠用做後續輸入命令,還能夠用做註釋,經常使用用法:開始註釋部分:輸入::<<eof 結束部分:eofblog

  具體示例以下所示:it

 1 # echo is test
 2 echo "test"
 3 echo "test"
 4 echo "test"
 5 echo "test"
 6 echo "test"
 7 :<<eof
 8 echo "comment"
 9 echo "comment"
10 echo "comment"
11 echo "comment"
12 echo "comment"
13 echo "comment"
14 eof

  運行結果:ast

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

 

2 感嘆號

  !號通常做爲嵌入內容部分,能夠用做註釋,經常使用用法:開始註釋部分:輸入::<<! 結束部分:!

  具體示例以下所示:

 1 # echo is test
 2 echo "test"
 3 echo "test"
 4 echo "test"
 5 echo "test"
 6 echo "test"
 7 :<<!
 8 echo "comment"
 9 echo "comment"
10 echo "comment"
11 echo "comment"
12 echo "comment"
13 echo "comment"
14 !

  運行結果:

➜  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

  自我感受,此次寫的比較清晰了,但願對你有用。

相關文章
相關標籤/搜索