bash bash中双方括号的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14496428/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:21:36  来源:igfitidea点击:

Meaning of double square brackets in bash

bash

提问by H2ONaCl

At this question in the answer from Kyle Brandtthe [[ ]]construct is described as a "bash built-in test command". The following is shown as an example.

Kyle Brandt 的回答中的这个问题中,[[ ]]构造被描述为“bash 内置测试命令”。下面是一个例子。

  if [[ ( $a -gt 2 ) && ( $a -lt 5 ) ]]; then ...

Why are double square brackets, literally [[ ]], necessary?

从字面上看[[ ]],为什么需要双方括号?

回答by vpillai

[[ is a much more versatile version of [ in bash.

[[ 是 bash 中更通用的 [ 版本。

For example, Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.

例如,使用 [[ ... ]] 测试构造而不是 [ ... ] 可以防止脚本中的许多逻辑错误。例如, &&、||、< 和 > 运算符在 [[ ]] 测试中工作,尽管在 [ ] 构造中给出错误。

also, their implementations are different

此外,他们的实现是不同的

-bash-3.2$ type [
[ is a shell builtin
-bash-3.2$ type [[
[[ is a shell keyword

Refer herefor a good explanation

请参阅此处以获得很好的解释

回答by DonCallisto

[[ ]]are equal to testkeyword.

[[ ]]等于test关键字。

You can also use [ ]but [[ ]]isn't compatible with all shell types

您也可以使用[ ][[ ]]与所有外壳类型不兼容

To me, if I understand the real sense of question, question itself isn't well-formulated.
Those brackets have to be here not so much for order determination, but because the bash synopsis require them

对我来说,如果我理解问题的真正含义,问题本身并没有很好地表述。
那些括号在这里不是为了确定顺序,而是因为 bash 概要需要它们

Edit

编辑

Question is changed, so my answer too.

问题变了,我的答案也变了。

[[ ]]is used against [ ]because you don't have to worry about quoting the left hand side of the test that will be read as a variable.
Moreover, <and >doesn't need to be escaped

[[ ]]用于反对[ ]是因为您不必担心引用将被读取为变量的测试的左侧。
此外,<>没有需要进行转义