bash Bash中单方括号和双方括号的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13542832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between single and double square brackets in Bash
提问by rkmax
I'm reading bash examples about if
but some examples are written with single square brackets:
我正在阅读有关 bash 的示例,if
但有些示例是用单个方括号编写的:
if [ -f $param ]
then
#...
fi
others with double square brackets:
其他带双方括号的:
if [[ $? -ne 0 ]]
then
start looking for errors in yourlog
fi
What is the difference?
有什么不同?
回答by cmh
Single []
are posix shell compliant condition tests.
单个[]
是符合 posix shell 的条件测试。
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.
Double[[]]
是标准的扩展,[]
并且被 bash 和其他 shell(例如 zsh、ksh)支持。它们支持额外的操作(以及标准的 posix 操作)。例如:||
代替-o
和正则表达式匹配=~
。有关条件结构的bash 手册部分提供了更完整的差异列表。
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.
使用[]
时,你想要你的脚本是跨弹便携。使用[[]]
,如果你想不被支持的条件表达式[]
,不需要是便携式的。
回答by sampson-chen
Inside single brackets for condition test (i.e. [ ... ]), some operators such as single =
is supported by all shells, whereas use of operator ==
is not supported by some of the older shells.
在条件测试的单括号内(即 [ ... ]),=
所有 shell 都支持诸如 single 之类的某些运算符,而==
某些较旧的 shell 不支持使用运算符。
Inside double brackets for condition test (i.e. [[ ... ]]), there is no difference between using =
or ==
in old or new shells.
在条件测试的双括号内(即 [[ ... ]]),使用=
或==
在旧壳或新壳中没有区别。
Edit: I should also note that: In bash, always use double brackets [[ ... ]] if possible, because it is safer than single brackets. I'll illustrate why with the following example:
编辑:我还应该注意:在 bash 中,如果可能,请始终使用双括号 [[ ... ]],因为它比单括号更安全。我将通过以下示例说明原因:
if [ $var == "hello" ]; then
if $var happens to be null / empty, then this is what the script sees:
如果 $var 恰好为 null / 空,那么这就是脚本所看到的:
if [ == "hello" ]; then
which will break your script. The solution is to either use double brackets, or always remember to put quotes around your variables ("$var"
). Double brackets is better defensive coding practice.
这会破坏你的脚本。解决方案是使用双括号,或者始终记住在变量 ( "$var"
)周围加上引号。双括号是更好的防御性编码实践。
回答by Gilles Quenot
[[
is a bash keyword similar to (but more powerful than) the [
command.
[[
是一个 bash 关键字,类似于(但比[
命令更强大)。
See
看
http://mywiki.wooledge.org/BashFAQ/031and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
http://mywiki.wooledge.org/BashFAQ/031和http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
Unless you're writing for POSIX sh, we recommend [[
.
除非您为 POSIX sh 编写代码,否则我们推荐[[
.
回答by asf107
you can use the double square brackets for light regex matching, e.g. :
您可以使用双方括号进行轻度正则表达式匹配,例如:
if [[ $1 =~ "foo.*bar" ]] ; then
if [[ $1 =~ "foo.*bar" ]] ; then
(as long as the version of bash you are using supports this syntax)
(只要您使用的 bash 版本支持此语法)
回答by user5500105
Bash manualsays:
Bash手册说:
When used with [[, the ‘<' and ‘>' operators sort lexicographically using the current locale. The test command uses ASCII ordering.
当与 [[ 一起使用时,'<' 和 '>' 运算符使用当前语言环境按字典顺序排序。测试命令使用 ASCII 排序。
(The test command is identical to [ ] )
(测试命令与 [ ] 相同)