Bash 中的布尔运算符(&&、-a、||、-o)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20449680/
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
Boolean operators ( &&, -a, ||, -o ) in Bash
提问by Victor Brunell
Can someone please explain the difference between the &&
, ||
, -a
, and -o
Unix operators?
是否有人可以解释之间的区别&&
,||
,-a
,和-o
Unix的运营商?
What are the restrictions on the usage of both types?
这两种类型的使用有什么限制?
Is it simply that the &&
and ||
operators should be used when using flags in the condition?
难道仅仅是在&&
和||
运营商应在条件使用标志时使用?
As in:
如:
[ "" = "yes" ] && [ -r .txt ]
versus:
相对:
[ "" = "yes" -a -lt 3 ]
回答by John Kugelman
Rule of thumb:Use -a
and -o
inside square brackets, &&
and ||
outside.
经验法则:使用-a
和-o
内方括号,&&
和||
外面。
It's important to understand the difference between shellsyntax and the syntax of the [
command.
了解shell语法和[
command语法之间的区别很重要。
&&
and||
are shell operators. They are used to combine the results of two commands. Because they are shell syntax, they have special syntactical significance and cannot be used as arguments to commands.[
is not special syntax. It's actually a command with the name[
, also known astest
. Since[
is just a regular command, it uses-a
and-o
for its andand oroperators. It can't use&&
and||
because those are shell syntax that commands don't get to see.
&&
和||
是外壳操作符。它们用于组合两个命令的结果。因为它们是 shell 语法,所以它们具有特殊的语法意义,不能用作命令的参数。[
不是特殊语法。它实际上是一个名为 的命令[
,也称为test
. 因为[
它只是一个常规命令,所以它使用-a
and-o
作为它的and和or运算符。它不能使用&&
,||
因为这些是命令看不到的 shell 语法。
But wait! Bash has a fancier test syntax in the form of [[ ]]
. If you use double square brackets, you get access to things like regexes and wildcards. You can also use shell operators like &&
, ||
, <
, and >
freely inside the brackets because, unlike [
, the double bracketed form isspecial shell syntax. Bash parses [[
itself so you can write things like [[ $foo == 5 && $bar == 6 ]]
.
可是等等!Bash 有一个更高级的测试语法,形式为[[ ]]
. 如果您使用双方括号,您可以访问诸如正则表达式和通配符之类的内容。您还可以在方括号内随意使用诸如&&
、||
、<
和 之类的 shell 运算符,>
因为与 不同[
,双方括号形式是特殊的 shell 语法。Bash 会[[
自行解析,因此您可以编写诸如[[ $foo == 5 && $bar == 6 ]]
.
回答by Brad Lanam
-a and -o are the older and/or operators for the test command. && and || are and/or operators for the shell. So (assuming an old shell) in your first case,
-a 和 -o 是 test 命令的较旧和/或运算符。&& 和 || 是和/或 shell 的运算符。所以(假设一个旧壳)在你的第一种情况下,
[ "" = 'yes' ] && [ -r .txt ]
The shell is evaluating the and condition. In your second case,
外壳正在评估和条件。在你的第二种情况下,
[ "" = 'yes' -a -lt 3 ]
The test command (or builtin test) is evaluating the and condition.
测试命令(或内置测试)正在评估和条件。
Of course in all modern or semi-modern shells, the test command is built in to the shell, so there really isn't any or much difference. In modern shells, the if statement can be written:
当然,在所有现代或半现代 shell 中,test 命令都内置在 shell 中,因此实际上没有任何区别或太大区别。在现代 shell 中,可以编写 if 语句:
[[ == yes && -r .txt ]]
Which is more similar to modern programming languages and thus is more readable.
这更类似于现代编程语言,因此更具可读性。