在Linux中,語句中的分號一般用作代碼塊標識
1、單行語句一般要用到分號來區分代碼塊,例如:
if [ "$PS1" ]; then echo test is ok; fi test is ok
該腳本或命令行中,需要兩個分號才為正確的語句,第一個分號是then前的分號,用於標識條件塊結束,第二個分號在fi前,用於標識then塊結束,如果缺少這兩個分號,則程序執行錯誤。
這里有趣的是echo后的字符串不需要使用引號也能正確地被識別。
注意:語句結尾不要分號。
2、該代碼若寫作多行,用換行符來區分代碼塊,則無需用到分號,例如:
if [ "PS1" ] > then echo "test is ok" > fi test is ok
從這個例子可看出if判斷語句分if塊,then塊,fi結束標識。當然還有可能有else if塊,例如:
if [ "$PS1" ] > then echo test is ok > elif [ "$PS2" ] > then echo here > fi test is ok
注意:這里要使用elif,而不使用esle if,若使用else if,則為不完整語句,如下面的例子不能被正確執行:
if [ "$PS1" ] > then echo test is ok > else if [ "$PS2" ] > then echo here > else > echo "" > fi >
敲回車后,shell程序認為句子沒有完成,繼續等待輸入。
總結:
如果寫成單行,需要用分號進行區分,如果寫成塊,那么則用換行符替代了分號。
參考:
http://blog.csdn.net/hongweigg/article/details/32081235(以上內容轉自此篇文章)