
if
我對,while
和語句中條件的預期值很好奇until
,因為我顯然無法讓我的條件正常工作。
他們是否期望true
和false
或退出代碼,即0
或{1-255}
?
答案1
條件測試退出程式碼。零為真,非零為假。該true
命令是正確的,因為它會傳回零 ( 0
) 退出代碼。類似地,false
返回一個非零退出代碼(碰巧,它是1
)。
if true; then echo 'happy days'; fi
if false; then echo 'maths has just died a horrible death'; fi
true; echo "exit status is $?" # "0"
false; echo "exit status is $?" # "1"
[ 0 -eq 0 ]; echo "exit status is $?" # "0"
[ 0 -eq 1 ]; echo "exit status is $?" # "1"