什么是单,双方括号中的Bash之间的区别? [英] What is the difference between single and double square brackets in Bash?
问题描述
我读的bash举例如果
,但一些例子单方括号写的:
I'm reading bash examples about if
but some examples are written with single square brackets:
if [ -f $param ]
then
#...
fi
别人用双括号:
if [[ $? -ne 0 ]]
then
start looking for errors in yourlog
fi
有什么区别呢?
推荐答案
单 []
是POSIX兼容的外壳条件测试。
Single []
are posix shell compliant condition tests.
双击 [[]]
是一个可扩展标准 []
和bash和其他shell都支持shell(例如zsh,ksh)中。它们支持额外的操作(以及标准的POSIX操作)。例如: ||
而不是 -o
和 =〜$正则表达式匹配C $ C>。差异更完整的列表可以在条件结构中的 bash的手册部分找到
Double [[]]
are an extension to the standard []
and are supported by bash and other shells (e.g. zsh, ksh). They support extra operations (as well as the standard posix operations). For example: ||
instead of -o
and regex matching with =~
. A fuller list of differences can be found in the bash manual section on conditional constructs.
只要你想你的脚本是可跨炮弹。使用 [[]]
如果你想有条件的前pressions不受支持[]
,不需要可移植。
Use []
whenever you want your script to be portable across shells. Use [[]]
if you want conditional expressions not supported by []
and don't need to be portable.
这篇关于什么是单,双方括号中的Bash之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!