Questions tagged [process-substitution]

Process substitution a form of inter-process communication that allows the input or output of a command to appear as a file (such as: `<(cmd)` or `>(cmd)`).

In bash manual we can read:

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.

Examples:

diff <(sort file1) <(sort file2)

Read more:

184 questions
125
votes
6 answers

Process substitution and pipe

I was wondering how to understand the following: Piping the stdout of a command into the stdin of another is a powerful technique. But, what if you need to pipe the stdout of multiple commands? This is where process substitution comes in. In…
Tim
  • 98,580
  • 191
  • 570
  • 977
59
votes
3 answers

What is the portable (POSIX) way to achieve process substitution?

Some shells, like bash, support Process Substitution which is a way to present process output as a file, like this: $ diff <(sort file1) <(sort file2) However, this construct isn't POSIX and, therefore, not portable. How can process substitution be…
starfry
  • 7,302
  • 6
  • 47
  • 69
57
votes
2 answers

Why does process substitution result in a file called /dev/fd/63 which is a pipe?

I am trying to understand named pipes in the context of this particular example. I type <(ls -l) in my terminal and get the output as, bash: /dev/fd/63: Permission denied. If I type cat <(ls -l), I could see the directory contents. If I replace the…
Ramesh
  • 38,687
  • 43
  • 140
  • 215
36
votes
1 answer

Why does BASH process substitution not work with some commands?

On occasion process substitution will not work as expected. Here is an example: Input: gcc <(echo 'int main(){return 0;}') Output: /dev/fd/63: file not recognized: Illegal seek collect2: error: ld returned 1 exit status Input: But it works as…
Lotney
  • 517
  • 1
  • 5
  • 8
32
votes
2 answers

Why does `sort <(ls -l)` work but `sort < (ls -l)` fail?

Today I'm learning something about fifo with this article: Introduction to Named Pipes, which mentions cat <(ls -l). I did some experiments by using sort < (ls -l), which pops out an error: -bash: syntax error near unexpected token `('` Then I…
Zen
  • 7,287
  • 18
  • 50
  • 72
31
votes
4 answers

grep files from list

I am trying to run grep against a list of a few hundred files: $ head -n 3 <(cat files.txt) admin.php ajax/accept.php ajax/add_note.php However, even though I am grepping for a string that I know is found in the files, the following does not search…
dotancohen
  • 15,494
  • 26
  • 80
  • 116
29
votes
5 answers

bash: how to propagate errors in process substitution?

I want my shell scripts to fail whenever a command executed with them fails. Typically I do that with: set -e set -o pipefail (typically I add set -u also) The thing is that none of the above works with process substitution. This code prints "ok"…
juanleon
  • 483
  • 4
  • 10
24
votes
4 answers

How to emulate Process Substitution in Dash?

In bash, I can use Process Substitution and treat output of a process as if it was a file saved on disk: $ echo <(ls) /dev/fd/63 $ ls -lAhF <(ls) lr-x------ 1 root root 64 Sep 17 12:55 /dev/fd/63 -> pipe:[1652825] unfortunately, Process…
Martin Vegter
  • 69
  • 66
  • 195
  • 326
22
votes
4 answers

How do I capture the exit code / handle errors correctly when using process substitution?

I have a script that parses file names into an array using the following method taken from a Q&A on SO: unset ARGS ARGID="1" while IFS= read -r -d $'\0' FILE; do ARGS[ARGID++]="$FILE" done < <(find "$@" -type f -name '*.txt' -print0) This works…
Glutanimate
  • 2,168
  • 4
  • 22
  • 38
19
votes
1 answer

In zsh, difference between cat <(cat) vs cat | cat vs cat =(cat)?

I expected cat <(cat) and cat | cat to do the same thing: copy lines from stdin to stdout. My understanding was that both would execute a cat in a subshell, redirect the subshell cat's stdout to a temporary named pipe, and then execute another cat…
Alan O'Donnell
  • 293
  • 2
  • 5
19
votes
2 answers

The process substitution output is out of the order

The echo one; echo two > >(cat); echo three; command gives unexpected output. I read this: How process substitution is implemented in bash? and many other articles about process substitution on the internet, but don't understand why it behaves…
MiniMax
  • 4,025
  • 1
  • 17
  • 32
18
votes
4 answers

CentOS 7: What is /bin/sh? It looks like Bash but seems to be something else

When I run a Centos 7 Docker Image like this docker run -it centos:7 bash Running something which is uses Process Substitution is fine (as expected, as Bash supports Process Substitution since the beginning of time - Bash 1.4.x actually). For…
helpermethod
  • 1,952
  • 2
  • 19
  • 25
18
votes
1 answer

Process substitution in GNU Makefiles

On a bash prompt, one can execute diff using pseudo files: diff <(echo test) <(echo test) Adding this as is into a Makefile fails: all: diff <(echo test) <(echo test) The error (hint: /bin/sh points to /bin/bash on this system): /bin/sh:…
Johannes
  • 353
  • 3
  • 13
18
votes
3 answers

How to combine Bash's process substitution with HERE-document?

In Bash version 4.2.47(1)-release when I try to catenate formatted text that comes from a HERE-dcoument like so: cat <(fmt --width=10 <
Tim Friske
  • 2,190
  • 3
  • 23
  • 36
17
votes
1 answer

Difference between subshells and process substitution

In bash, I want to assign my current working directory to a variable. Using a subshell, I can do this. var=$(pwd) echo $var /home/user.name If I use process substitution like so: var=<(pwd) echo $var /dev/fd/63 I have understood that process…
PejoPhylo
  • 335
  • 3
  • 6
1
2 3
12 13