用||回显和&& [英] echo with || and &&

查看:72
本文介绍了用||回显和&&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash上尝试了以下命令

I tried the following command on bash

echo this || echo that && echo other

这给出了输出

this
other

我不明白!

我的空跑是这样的:

  1. echo this || echo that && echo other表示true || true && true
  2. 由于&&||具有more precedence,因此第二个表达式优先计算
  3. 由于both are true,对||的求值也为true.
  4. 因此,我得出的结论是:
  1. echo this || echo that && echo other implies true || true && true
  2. Since, && has more precedence than ||, 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:

那个

其他

这个

由于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆