Rya*_*ert 3 parameters bash optional-parameters parameter-expansion
因此,传统上,当我使用参数标志编写bash脚本时,我使用基本的测试语句实现默认值
if [ -z $foo ]
then
foo=$bar
fi
Run Code Online (Sandbox Code Playgroud)
今天我遇到了更高级的参数扩展,似乎做了同样的事情
${foo:=$bar}
Run Code Online (Sandbox Code Playgroud)
这两种方法比较如何?它们的优点/缺点是什么?
编辑:修复了评论中指出的一些拼写错误
典型的习语是
: ${foo:=$bar}
Run Code Online (Sandbox Code Playgroud)
作为替代品
if [ -z "$foo" ]
then
foo=$bar
fi
Run Code Online (Sandbox Code Playgroud)
(注意引号和空格!)
在前者中,参数扩展处理其他do-nothing命令的赋值.它更简洁,但除此之外没有理由选择其中一个.请注意,POSIX都支持这两种方法.
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |