寻找一些bash专家的解释.下一个之间的确切区别是什么
command1 | command2
Run Code Online (Sandbox Code Playgroud)
例如,经典管道,其中command1的stdout被重定向到command2的stdin,例如
和
command1 > >(command2)
Run Code Online (Sandbox Code Playgroud)
结果(和bash动作)是一样的......
至少我得到了相同的
find . -print0 | xargs -0 -n1 -I% echo =%=
Run Code Online (Sandbox Code Playgroud)
和
find . -print0 > >(xargs -0 -n1 -I% echo =%=)
Run Code Online (Sandbox Code Playgroud)
是> >(command)唯一更长的说法|吗?或者不是,我错过了什么?
实施
command1 | command2shell在父进程中创建一个管道,并将其一端附加到command1的输出(fd 1;它使用dup或dup2),另一端连接到command2的输入(fd 0).
实施
command1 > >(command 2),shell创建一个FIFO.它将command2的stdin附加到FIFO(通常使用O_WRONLY标志打开),并将FIFO的名称作为command1的位置参数传递.您可以通过使用轻松地看到这一点
echo >(true).
如果你使用
> >(foo)这些形式确实非常相似.但是,流程替代机制更加强大.例如,你做了这样的事情:
diff -u <(curl 'http://www.a.example.com/') <(curl 'http://www.b.example.com/')
你不能用管道做到这一点 - 你不能有两个标准输入.
我的问题中至少讨论了部分差异。
另一个区别是控制哪些进程在子 shell 中运行:
$ declare -p f b
-bash: declare: f: not found
-bash: declare: b: not found
$ { f=foo; true; } | { b=bar; true; }
$ declare -p f b
-bash: declare: f: not found
-bash: declare: b: not found
$ { f=foo; true; } > >( { b=bar; true; })
$ declare -p f b
declare -- f="foo"
-bash: declare: b: not found
Run Code Online (Sandbox Code Playgroud)