3

From the bash manual

Process substitution is supported on systems that support named pipes (fifos) or the /dev/fd method of naming open files. It takes the form of

<(list)

or

>(list)

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.

Is the command list in a process substitution <(list) or >(list) invoked in a subshell, similarly to a command substitution, commands grouped with parentheses, and asynchronous commands? Similar as the bash manual says

Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation.

  1. The answer might be yes, because

    • process substitution looks similar to command substitution,

    • some source says that

      the command inside it is run in the background.

    and the above second quote from the bash manual says that both command substitution and backgrounded commands are invoked in subshells.

  2. The answer might be no, because

    • In the bash manual, I didn't see that a process substitution is mentioned in the above second quote from the bash manual,

    • and some source says that process substitution

      is especially important for bypassing subshells caused by pipelines

      although I am not sure if "bypassing subshells" means not being invoked in a subshell.

Tim
  • 101,790

1 Answers1

3

Running the current shell under strace(1) and then executing e.g. <(command) gives:

clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fa6713d59d0) = 13305

From a purely definitional standpoint, since clone(2) is defined as

create a child process

and a subshell as

Running a shell script launches a new process, a subshell.

one could say that yes - running process substitution is invoked as a subshell.

ckujau
  • 1,403
  • I ran the following in a terminal: a=$$; b=$(cat <(echo $$)); if [ $a -eq $b ]; then echo "NOT in a subshell"; else echo "in a subshell"; fi. The output was: NOT in a subshell – Harry Jun 16 '18 at 10:18
  • @Harry I think that is happening there is that all the $$ get evaluated by the top-level shell. – Tom Anderson Oct 29 '18 at 17:50
  • But compare the output of pstree -p $$, echo $(pstree -p $$), (pstree -p $$), and (echo $(pstree -p $$)) - on my machine, only the process substitution inside the subshell actually spawns another subshell. – Tom Anderson Oct 29 '18 at 17:53