带括号和方括号的 IF 语句在 Bash 中的区别

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

Difference in Bash between IF statements with parenthesis and square brackets

bashif-statement

提问by Luis Alvarado

While learning a bit about bash, I come to see four types of ways of working with ifstatements:

在学习bash 的同时,我看到了四种处理if语句的方法:

  • Single Parenthesis - ( ... )
  • Double Parenthesis - (( ... ))
  • Single Square Bracket - [ ... ]
  • Double Square Brackets - [[ ... ]]
  • 单括号 - ( ... )
  • 双括号 - (( ... ))
  • 单方括号 - [ ... ]
  • 双方括号 - [[ ... ]]

What is the difference between Parenthesis and Square Brackets in bash.

bash 中的括号和方括号有什么区别。

回答by Gilles Quenot

The tests you had listed :

您列出的测试:

  • Single Parenthesis - ( ... ) is creating a subshell
  • Double Parenthesis - (( ... )) is for arithmetic operation
  • Single Square Bracket - [ ... ] is the syntax for the POSIX test
  • Double Square Brackets - [[ ... ]] is the syntax for bash conditional expressions (similar to testbut more powerful)
  • 单括号 - ( ... ) 正在创建一个子外壳
  • 双括号 - (( ... )) 用于算术运算
  • 单方括号 - [ ... ] 是 POSIX 的语法 test
  • 双方括号 - [[ ... ]] 是 bash 条件表达式的语法(类似于test但更强大)

are not exhaustive, you can use boolean logic

并非详尽无遗,您可以使用布尔逻辑

if command; then ...

too, because the commands have exit status. In bash, 0is trueand > 0is false.

也是,因为命令具有退出状态。在bash,0true并且 >0false

You can see the exit status like this :

你可以看到这样的退出状态:

command
echo $?

See :

看 :

http://wiki.bash-hackers.org/syntax/basicgrammar
http://wiki.bash-hackers.org/syntax/arith_expr
http://mywiki.wooledge.org/BashGuide/TestsAndConditionals

http://wiki.bash-hackers.org/syntax/basicgrammar
http://wiki.bash-hackers.org/syntax/arith_expr
http://mywiki.wooledge.org/BashGuide/TestsAndConditionals

回答by tripleee

The shell itself only runs the command and evaluates its exit code. A zero exit code signifies success; all other values indicate failure.

shell 本身只运行命令并评估其退出代码。零退出代码表示成功;所有其他值表示失败。

if command; then
    : things to do if the exit code from command was 0
else
    : things to do if it was not 0
fi

while command; do
    : things to do if the exit code was 0
done

The command [(aka test) is very commonly used in conditionals, because the original Bourne shell lacked built-in operators to check if a string was empty or a file existed. Modern shells have this command built in, and many shells have an extended and modernized version [[, but this is not properly portable to POSIX shand should thus be avoided for portable scripts. This related questionexplains the differences between the two in more detail.

命令[(又名test)在条件语句中非常常用,因为原始的 Bourne shell 缺少用于检查字符串是否为空或文件是否存在的内置运算符。现代 shell 内置了此命令,并且许多 shell 具有扩展和现代化版本[[,但这不能正确移植到 POSIX sh,因此应避免用于可移植脚本。这个相关问题更详细地解释了两者之间的差异。

The notation (( ... ))introduces an arithmetic context. Again, this was something which was not part of the original Bourne shell (it had a dedicated external tool exprfor these things) but modern shells have this built in. The result code of an arithmetic expression is 0 if the result of the arithmetic evaluation was not 0 (or an error).

该符号(( ... ))引入了算术上下文。同样,这不是原始 Bourne shell 的一部分(它有一个专门的外部工具expr来处理这些事情)但是现代 shell 内置了这个。如果算术评估的结果是,算术表达式的结果代码是 0不是 0(或错误)。

The notation ( command )creates a subshell and evaluates commandin that. There are situations where this is actually necessary and useful, but if you are only just learning the syntax, you are unlikely to need this.

该符号( command )创建一个子外壳并command在其中进行评估。在某些情况下这实际上是必要和有用的,但如果您只是学习语法,则不太可能需要它。

... In fact, in the majority of scripts I have seen this used in a conditional, it was clearly unnecessary.

... 事实上,在我看到的大多数脚本中,这是在条件中使用的,这显然是不必要的。

Another antipattern to look out for is

另一个需要注意的反模式是

command
if [ $? = 0 ]; then
    : things
fi

You should almost never need to examine $?explicitly, and in particular, comparing it to zero is something ifand whilespecificallydo for you behind the scenes. This should simply be refactored to

你应该几乎从来没有需要检查$?明确,尤其是,它与零进行比较的东西if,并while专门为你做幕后。这应该简单地重构为

if command; then
    : ...