什么是单,双方括号中的Bash之间的区别? [英] What is the difference between single and double square brackets in Bash?

查看:191
本文介绍了什么是单,双方括号中的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 =〜。差异更完整的列表可以在条件结构中的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆