shell 腳本中的注釋詳解


  上次寫了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

  具體示例如下所示:

 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

  運行結果:

➜  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

  自我感覺,這次寫的比較清晰了,希望對你有用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM