對於set命令-o參數的pipefail選項,linux是這樣解釋的:
“If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands in the pipeline exit successfully. This option is disabled by default.”
設置了這個選項以後,包含管道命令的語句的返回值,會變成最後一個返回非零的管道命令的返回值。聽起來比較繞,其實也很簡單:
set命令的-e參數,linux自帶的說明如下:
“Exit immediately if a simple command exits with a non-zero status.”
也就是說,在”set -e”之後出現的代碼,一旦出現了返回值非零,整個腳本就會立即退出。有的人喜歡使用這個參數,是出於保證代碼安全性的考慮。但有的時候,這種美好的初衷,也會導致嚴重的問題。
set -x 與 set +x命令的作用實際是用於輸出詳細日誌,是Shell腳本中使用echo命令輸出的替代方案。更適用於輸出大量日誌的場景使用
set -x 是開啓,set +x是關閉,set -o是查看 (xtrace,追蹤一段代碼的顯示情況)