What is happening behind bash forward process substitution?

bash, process

Solution

From `Process Substitution`

The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list.

What happens with `echo "test" >(wc)`?

The file `/dev/fd/63` is opened for connecting `echo test` with `wc`. `wc` is started with it's input connected to `/dev/fd/63`. Then the name of this file (`/dev/fd/63`) is passed as an argument to the current command (`echo "test"`), resulting in `echo "test" /dev/fd/63`. This is why you see

test /dev/fd/63

as output. `wc` waits for input, but since `echo` doesn't write to `/dev/fd/63`, the count will be `0`.

If you want this to work, you must create a script, which takes the last argument and echos the first `N-1` arguments into the last

#! /bin/bash
echo "${@:1:$(($# - 1))}" >${@: -1}

When you call this

bash script.sh test >(wc)

you will see the expected output

1 1 5

Problem

According to the wikipedia, "Process substitution can also be used to capture output that would normally go to a file, and redirect it to the input of a process."(http://en.wikipedia.org/wiki/Process_substitution). So, in my own words, that means with process substitution, I can take the output of the command A and use it as the input of the command B. In other words, it's like a pipe(is this correct?). So if this true, and if I do this: ``` echo "test" >(wc) ``` then I should expect to get the following: ``` 1 1 5 ``` because my understanding of the above command is similar to the following: ``` $echo "test" > tmp $wc tmp 1 1 5 tmp ``` except I don't make the tmp file with the process substitution. But instead I get the following output: ``` test /dev/fd/63 ``` This obviously indicates somehow my mental model is incorrect. Where am I wrong? I understand the <(command). For example ``` $diff <(head file1) <(head file2) ``` perfectly makes sense. but not >(command).

Original source