0

Is the usage of process substitution

command1 <(command2)

equivalent to

command2 > ~/myfile
command1 ~myfile

?

Can the two above be replaced with each other?

Does command1 and command2 in the usage of command substitution always run simultaneously, while in the "replacement" command1 runs only after command2 finishes running?

If yes, does this difference make the two not equivalent and not always replaced with each other in some cases?

Thanks.

7

This is process substitution.

The end result of both forms you give should be mostly the same; the differences are that

  • as you point out, process substitution runs both processes simultaneously;
  • your second form uses a file on a file system, so you need space to store that (and ideally, remove it afterwards).

Technically, the I/O between processes involving process substitution behaves like pipes, not like files. (This might well be an implementation detail though.)

Correctly-written programs shouldn’t exhibit differences in their results because they run simultaneously; for example,

tail -n 1 <(seq 1 10000000)

correctly shows the last number in the sequence, whereas attempting to do this manually with

seq 1 10000000 > blah &
tail -n 1 blah

will fail because tail shows whatever is the last line stored at the time it runs, it doesn’t wait for seq to finish. Your second form correctly waits in this instance.

You can however see differences in external behaviour. For example,

head -n 1 <(seq 1 1000000000)

finishes immediately (even seq, thanks to the pipe behaviour), whereas

seq 1 1000000000 > blah
head -n 1 blah

takes quite a long time to run. This type of difference can mean that replacing one form with the other isn’t appropriate.

  • I've never looked into the source to see which bash prefers to use, but the man page documents process substitution as requiring either named pipes or /dev/fd. – chepner Aug 1 '17 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.