用||回显和&& [英] echo with || and &&
问题描述
我在bash上尝试了以下命令
I tried the following command on bash
echo this || echo that && echo other
这给出了输出
this
other
我不明白!
我的空跑是这样的:
-
echo this || echo that && echo other
表示true || true && true
- 由于
&&
比||
具有more precedence
,因此第二个表达式优先计算 - 由于
both are true
,对||
的求值也为true. - 因此,我得出的结论是:
echo this || echo that && echo other
impliestrue || true && true
- Since,
&&
hasmore precedence
than||
, the second expression evaluates first - Since,
both are true
, the||
is evaluated which also gives true. - Hence, I conclude the output to be:
那个
其他
这个
由于Java背景中&&
的优先级高于||
,所以我无法将其与bash相关.
Being from a Java background where &&
has more precedence than ||
, I am not able to relate this to bash.
任何输入都会很有帮助!
Any inputs would be very helpful!
推荐答案
来自 man bash
3.2.3命令列表
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.
所以,你的例子
echo this || echo that && echo other
可以读为
(this || that) && other
这篇关于用||回显和&&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!