3

I guess I'm missing some understanding on the use cases of a process substitution. My intuition was that a process substitution, of the form <(COMMANDS) would execute COMMANDS and then feed the result of the program into whatever command it's a part of, so command1 <(command2) would evaluate command2 and pass the result as the first argument into command1.

I thought the following would've worked:

$ for i in <(cat list.txt); do echo $i; done 

where list.txt is a file containing a list of words (separated by newlines). When I run this, it simply outputs /dev/fd/63, which I can only assume is like a temporary pathname to the output of the subshell created in the process substitution?

I thought the above would've worked, because it works fine when I write

$ for i in `cat list.txt`; do echo $i; done 

I've never seen this ` notation before, what does it mean exactly? And what understanding am I lacking about process substitutions?

1 Answer 1

8

`foo` is command substitution, not process substitution. $(foo) is also command substitution, and is the preferred form since it is easier to use nested command substitution: $(foo1 $(foo2 $(foo3 ...))).

With command substitution, `foo`/$(foo), the output of foo is used as words in the command line. So for i in $(echo a b c) becomes as if you'd used for i in a b c instead. The command in the command substitution is executed first, its output obtained, and then the output is used to create the next command line, which is then executed, and so on. This allows the use of a command or function to generate the list that will be iterated over by the for loop. Field splitting, wildcard expansion, etc. happen, so quoting is an important consideration in command substitution.

With process substitution, <(foo)/>(foo), the stdin/stdout of the process is provided as a file, so cat <(foo) becomes as if you'd used foo > /some/file and cat /some/file, and tee >(foo) becomes as if you'd done tee /some/file and foo < /some/file. The commands are executed concurrently. Since the output is not seen by the shell, field splitting and wildcard expansion are not a concern.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.