我在bash上尝试了以下命令
echo this || echo that && echo other
Run Code Online (Sandbox Code Playgroud)
这给出了输出
this
other
Run Code Online (Sandbox Code Playgroud)
我不明白!
我的干跑是这样的:
echo this || echo that && echo other
暗示 true || true && true
&&
具有more precedence
比||
,所述第二表达式计算第一both are true
,在||
评价这也给了真.那
其他
这个
从&&
具有更多优先级的Java背景来看||
,我无法将其与bash联系起来.
任何输入都会非常有用!
Ola*_*che 13
从 man bash
3.2.3命令列表
列表是由一个运算符';','&','&&'或'||'分隔的一个或多个管道的序列,并且可选地以';','&'或a之一终止新队.
在这些列表运算符中,'&&'和'||' 有相同的优先权,其次是';' 和'&',具有相同的优先权.
那么,你的榜样
echo this || echo that && echo other
Run Code Online (Sandbox Code Playgroud)
可以像
(this || that) && other
Run Code Online (Sandbox Code Playgroud)
在 bash 中,&&
和||
具有同等优先级并关联到左侧。详细信息请参见手册第3.2.3 节。
所以,你的例子被解析为
$ (echo this || echo that) && echo other
Run Code Online (Sandbox Code Playgroud)
因此,只有 or 的左侧运行,因为它成功了,右侧不需要运行。