Bash 条件中括号和括号之间的区别 [英] Difference between parentheses and brackets in Bash conditionals

查看:47
本文介绍了Bash 条件中括号和括号之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习一些关于 的同时,我来看看四种使用 if 语句的方法:

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

  • 单括号 - ( ... )
  • 双括号 - (( ... ))
  • 单方括号 - [ ... ]
  • 双方括号 - [[ ... ]]

bash 中括号和方括号有什么区别.

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

推荐答案

您列出的测试:

  • 单括号 - ( ... ) 正在创建一个子外壳
  • 双括号 - (( ... )) 用于算术运算
  • 单方括号 - [ ... ] 是 POSIX test
  • 的语法
  • 双方括号 - [[ ... ]] 是 bash 条件表达式的语法(类似于 test 但更强大)
  • 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 test but more powerful)

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

are not exhaustive, you can use boolean logic

if command; then ...

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

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

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

You can see the exit status like this :

command
echo $?

见:

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

这篇关于Bash 条件中括号和括号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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