Bash条件中括号和括号之间的区别 [英] Difference between parentheses and brackets in Bash conditionals
本文介绍了Bash条件中括号和括号之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在学习一些有关 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.
推荐答案
您列出的测试:
- 单括号-(...)正在创建子shell
- 双括号-((... ...))用于算术运算
- 单方括号-[...]是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)
并不详尽,可以使用布尔逻辑
if command; then ...
也是,因为命令具有退出状态.在 bash
中, 0
为 true
,> 0
为 false
.
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屋!
查看全文