bash 用 || 回声 和 &&

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

echo with || and &&

linuxbashshell

提问by TechSpellBound

I tried the following command on bash

我在 bash 上尝试了以下命令

echo this || echo that && echo other

This gives the output

这给出了输出

this
other

I din't understand that!

我不明白!

My dry run goes this way :

我的试运行是这样的:

  1. echo this || echo that && echo otherimplies true || true && true
  2. Since, &&has more precedencethan ||, the second expression evaluates first
  3. Since, both are true, the ||is evaluated which also gives true.
  4. Hence, I conclude the output to be:
  1. echo this || echo that && echo other暗示 true || true && true
  2. 因为,&&more precedence||,第二个表达式首先计算
  3. 因为,both are true||被评估也给出真。
  4. 因此,我得出的结论是:

that

other

this

其他

这个

Being from a Java background where &&has more precedence than ||, I am not able to relate this to bash.

来自 Java 背景,&&优先级高于||,我无法将其与 bash 联系起来。

Any inputs would be very helpful!

任何输入都会非常有帮助!

回答by Olaf Dietsche

From man bash

man bash

3.2.3 Lists of Commands

A list is a sequence of one or more pipelines separated by one of the operators ‘;', ‘&', ‘&&', or ‘||', and optionally terminated by one of ‘;', ‘&', or a newline.

Of these list operators, ‘&&' and ‘||' have equal precedence, followed by ‘;' and ‘&', which have equal precedence.

3.2.3 命令列表

列表是由运算符“;”、“&”、“&&”或“||”之一分隔的一个或多个管道的序列,并且可选地以“;”、“&”或a新队。

在这些列表运算符中,“&&”和“||” 具有相同的优先级,后跟 ';' 和 '&',它们具有相同的优先级。

So, your example

所以,你的例子

echo this || echo that && echo other

could be read like

可以这样读

(this || that) && other

回答by unwind

In bash, &&and ||have equalprecendence and associate to the left. See Section 3.2.3 in the manual for details.

在 bash 中,&&||具有相同的优先级并与左侧关联。有关详细信息,请参阅手册中的第 3.2.3 节

So, your example is parsed as

所以,你的例子被解析为

$ (echo this || echo that) && echo other

And thus only the left-hand side of the or runs, since that succeeds the right-hand side doesn't need to run.

因此只有 or 的左侧运行,因为它成功了右侧不需要运行。

回答by lanzz

Boolean evaluation in bashis short-circuit: true || falsewill never evaluate the falseoperand, because the trueoperand is enough to determine the outcome of the operation. Likewise, false && truewill not evaluate the trueoperand, because it cannot change the value of the expression.

布尔求值 in bashis short-circuit:true || false永远不会求值false操作数,因为true操作数足以决定操作的结果。同样,false && true不会对true操作数求值,因为它不能改变表达式的值。

Boolean evaluation in bashis actually used mainlyfor controlling the conditional evaluation of the operands, not their order. Typical usage is do_foo || do_bar_if_foo_failsor do_foo && do_bar_only_if_foo_has_succeeded.

布尔求bash值实际上主要用于控制操作数的条件求值,而不是它们的顺序。典型用法是do_foo || do_bar_if_foo_failsdo_foo && do_bar_only_if_foo_has_succeeded

In no situation will your echo thatcommand be executed, because the echo thisis trueand determines the value of the entire echo this || echo thatsub-expression.

在任何情况下都echo that不会执行您的命令,因为echo thisistrue和 决定了整个echo this || echo that子表达式的值。

回答by Anton Kovalenko

From man bash:

来自man bash

Of these list operators, && and || have equal precedence, followed by ; and &, which have equal precedence.

在这些列表运算符中,&& 和 || 具有相同的优先级,其次是 ; 和 & 具有相同的优先级。

So your output is expected.

所以你的输出是预期的。

回答by Carl Norum

I think you've already figured it out. Operator precedence doesn't work like that in bash. Everything just goes left to right in your example.

我想你已经想通了。运算符优先级在bash. 在你的例子中,一切都是从左到右的。

回答by Naryl

Let's explain what this does:

让我们解释一下这是做什么的:

echo this || echo that && echo other

echo this || echo that-> echo thatonly if echo thisFAILS.

echo this || echo that->echo that仅当echo this失败时。

&& echo other-> echo otheronly if the command before &&SUCCEEDS.

&& echo other->echo other仅当命令在&&SUCCEEDS之前。

So basically:

所以基本上:

echo this---> SUCCESS ----> echo that----> is not executed since echo thissucceeded ---> echo other---> is executed cause echo this || echo thatwas executed correctly.

echo this---> SUCCESS ----> echo that----> 由于echo this成功而未执行---> echo other---> 已执行,原因echo this || echo that是正确执行。