管道 |重定向<&gt;优先级 [英] Pipe | Redirection &lt; &gt; Precedence

查看:26
本文介绍了管道 |重定向<&gt;优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明确何时使用管道 |或重定向 <> 在命令中优先?

I want to make clear when does pipe | or redirection < > takes precedence in a command?

这是我的想法,但需要确认它是如何工作的.

This is my thought but need confirmation this is how it works.

示例 1:

sort < names | head
The pipe runs first:  names|head   then it sorts what is returned from names|head

示例 2:

ls | sort > out.txt
This one seems straight forward by testing, ls|sort then redirects to out.txt

示例 3:

Fill in the blank?  Can you have both a < and a > with a | ???

推荐答案

在语法分组方面,><优先级更高;也就是说,这两个命令是等价的:

In terms of syntactic grouping, > and < have higher precedence; that is, these two commands are equivalent:

sort < names | head
( sort < names ) | head

这两个也是:

ls | sort > out.txt
ls | ( sort > out.txt )

但是在顺序上,|是先执行的;所以,这个命令:

But in terms of sequential ordering, | is performed first; so, this command:

cat in.txt > out1.txt | cat > out2.txt

将填充 out1.txt,而不是 out2.txt,因为 >out1.txt | 之后执行,因此 取代 它(因此没有输出通过管道输出到 cat>out2.txt).

will populate out1.txt, not out2.txt, because the > out1.txt is performed after the |, and therefore supersedes it (so no output is piped out to cat > out2.txt).

同样,这个命令:

cat < in1.txt | cat < in2.txt

将打印 in2.txt,而不是 in1.txt,因为 <in2.txt | 之后执行,因此 取代 它(因此没有输入从 cat 输入).

will print in2.txt, not in1.txt, because the < in2.txt is performed after the |, and therefore supersedes it (so no input is piped in from cat < in1.txt).

这篇关于管道 |重定向<&gt;优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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