且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bash 条件中括号和括号之间的区别

更新时间:2022-01-27 23:03:02

您列出的测试:

  • 单括号 - ( ... ) 正在创建一个子外壳
  • 双括号 - (( ... )) 用于算术运算
  • 单方括号 - [ ... ] 是 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