1. 程式人生 > >set -e作用

set -e作用

#!/bin/bash

set -e

command 1
command 2
...

exit 0
----------------------------------------------------------

Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit.

你寫的每個指令碼都應該在檔案開頭加上set -e,這句語句告訴bash如果任何語句的執行結果不是true則應該退出。這樣的好處是防止錯誤像滾雪球般變大導致一個致命的錯誤,而這些錯誤本應該在之前就被處理掉。如果要增加可讀性,可以使用set -o errexit,它的作用與set -e相同。