我正在關注變量變換的this教程。
如果我有以下幾點:
echo ${TEST:-test} #TEST is undefined, 'test' is printed and TEST is still undefined.
echo ${FOO:?"some text"} #"some text" is printed and FOO is still undefined.
是什麼':-'
及以上':?'
之間的區別?
我正在關注變量變換的this教程。
如果我有以下幾點:
echo ${TEST:-test} #TEST is undefined, 'test' is printed and TEST is still undefined.
echo ${FOO:?"some text"} #"some text" is printed and FOO is still undefined.
是什麼':-'
及以上':?'
之間的區別?
這些是測試快捷方式:
echo ${TEST:-test}
如果$TEST
存在,那麼將使用它的值,否則將被使用的$test
值。如果你想TEST
要設置那麼你可能需要:
echo ${TEST:=test}
下一個:
echo ${FOO:?"some text"}
如果$FOO
設置,那麼使用它的值,否則輸出到stderr
錯誤消息「一些文本」(默認爲「參數爲空或未設置」)。
稍作更正:':'和':='將同時使用如果設置了「TEST」,則爲備用值,但是爲空字符串。 '$ {TEST-test}'和'$ {TEST = test}'將保留空字符串,如果這是'TEST'之前明確設置的。 '$ {FOO:?}'同樣會打印錯誤信息,如果設置了'FOO'但是爲空字符串;如果一個空值是有效的,使用'$ {FOO?error}'。 – chepner
你在這裏有你的答案:https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html – codeforester