Pipe against redirection to process

Look for a description of bash -expert . What is the difference between the following

command1 | command2 

eg. classic pipe where stdout of command 1 is redirected to stdin of command2, for example.

  • bash deploys itself twice
  • changes file descriptors
  • execute command comman1 and command2

and

 command1 > >(command2) 

where the result (and bash actions) are the same ...

At least I got the same from

 find . -print0 | xargs -0 -n1 -I% echo =%= 

and

 find . -print0 > >(xargs -0 -n1 -I% echo =%=) 

Is > >(command) just a longer way by saying | ? Or not, and did I miss something?

+5
source share
2 answers

To implement

  command1 |  command2 
, the shell creates a channel in the parent process and attaches one end to the output (fd 1; uses dup or dup2) of command 1, and the other end of the input (fd 0) of command 2.

To implement

  command1>> (command 2) 
, the shell creates a FIFO. It attaches command2 to the FIFO file (usually open using the O_WRONLY flag) and passes the FIFO name as a positional argument to command1. You can easily see this using echo >(true) .

If you use

  >> (foo) 
, these forms are really very similar. However, the process subsection mechanism is more efficient. For example, you do things like this:
  diff -u <(curl 'http://www.a.example.com/') <(curl 'http://www.b.example.com/') 

You cannot do this with pipes - you cannot have two standard inputs.

+7
source

At least part of the difference is discussed in my question .

An additional difference is the control over which processes are performed in sub-shells:

 $ declare -pfb -bash: declare: f: not found -bash: declare: b: not found $ { f=foo; true; } | { b=bar; true; } $ declare -pfb -bash: declare: f: not found -bash: declare: b: not found $ { f=foo; true; } > >( { b=bar; true; }) $ declare -pfb declare -- f="foo" -bash: declare: b: not found 
+4
source

Source: https://habr.com/ru/post/1201021/


All Articles