Questions tagged [process-substitution]

Process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell. This allows programs that normally only accept files to directly read from or write to another program.

129 questions
97
votes
5 answers

Capture stdout to a variable but still display it in the console

I have a bash script which calls several long-running processes. I want to capture the output of those calls into variables for processing reasons. However, because these are long running processes, I would like the output of the rsync calls to be…
Mendhak
  • 8,194
  • 5
  • 47
  • 64
21
votes
3 answers

Bash process substitution and syncing

(Possibly related to Do some programs not accept process substitution for input files?) In some Bash unit test scripts I'm using the following trick to log and display stdout and stderr of a command: command > >(tee "${stdoutF}") 2> >(tee…
l0b0
  • 55,365
  • 30
  • 138
  • 223
17
votes
4 answers

Get exit code of process substitution with pipe into while loop

The following script calls another program reading its output in a while loop (see Bash - How to pipe input to while loop and preserve variables after loop ends): while read -r col0 col1; do # [...] done < <(other_program [args ...]) How can I…
Philipp H.
  • 1,513
  • 3
  • 17
  • 31
17
votes
4 answers

Command substitution vs process substitution

I am trying to understand the differences between these two similar commands. aa=$(foo | bar | head -1) read aa < <(foo | bar | head -1) I know that <() requires #!/bin/bash, but does that make it slower? Do they create the same amount of…
Zombo
  • 1
  • 62
  • 391
  • 407
15
votes
1 answer

Why source command doesn't work with process substitution in bash 3.2?

I've the following shell script: cat <(echo foo) source <(echo bar=bar) echo $bar However it works differently in GNU bash 3.2 and 4.3 as shown below: $ /bin/bash foo.sh foo 3.2.53(1)-release $ /usr/local/bin/bash foo.sh…
kenorb
  • 155,785
  • 88
  • 678
  • 743
14
votes
5 answers

How to find next available file descriptor in Bash?

How can I figure out if a file descriptor is currently in use in Bash? For example, if I have a script that reads, writes, and closes fd 3, e.g. exec 3< <(some command here) ... cat <&3 exec 3>&- what's the best way to ensure I'm not interfering…
Kvass
  • 8,294
  • 12
  • 65
  • 108
14
votes
1 answer

POSIX shell equivalent to <()

<(commands ...) in bash/zsh makes the output behavior as a file. Does a POSIX equivalent exist?
Sandlayth
  • 185
  • 8
12
votes
4 answers

Syntax error in shell script with process substitution

I have this shell script which I use to back up my system. There is a line: tar -Pzcpf /backups/backup.tar.gz --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log / 2> >(grep -v 'socket ignored' >&2) As…
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
11
votes
2 answers

Why does `cat <(cat)` produce EIO?

I have a program that reads from two input files simultaneously. I'd like to have this program read from standard input. I thought I'd use something like this: $program1 <(cat) <($program2) but I've just discovered that cat…
i336_
  • 1,813
  • 1
  • 20
  • 41
10
votes
3 answers

Process substitution capture stderr

For this question I will use grep, because its usage text prints to stderr: $ grep Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. You can capture stdout easily with process substitution: $ read b < <(echo hello…
Zombo
  • 1
  • 62
  • 391
  • 407
9
votes
2 answers

What is the difference between using process substitution vs. a pipe?

I came across an example for the using tee utility in the tee info page: wget -O - http://example.com/dvd.iso | tee >(sha1sum > dvd.sha1) > dvd.iso I looked up the >(...) syntax and found something called "process substitution". From what I…
Ashton Wiersdorf
  • 1,865
  • 12
  • 33
8
votes
1 answer

bash "wc -l" command output differs if call or through tee

When I issued two equivalent commands in Bash I got different output (from "wc -l" command), see below: root@devel:~# ls /usr/bin -lha | tee >(wc -l) >(head) > /dev/null total 76M drwxr-xr-x 2 root root 20K Nov 11 18:58 . drwxr-xr-x 10 root…
urusai_na
  • 393
  • 4
  • 11
7
votes
3 answers

Bash setting a global variable inside a loop and retaining its value -- Or process substituion for dummies

I'm a C/C++ programmer and quite stupid in general (or at least the way bash does things it makes me feel confused). I can't wrap my head around process substitution. I need to define a global boolean, set it somewhere in a loop, and make use of it…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
6
votes
6 answers

bash: How do I ensure termination of process substitution used with exec?

If I run $#/bin/bash for i in `seq 5`; do exec 3> >(sed -e "s/^/$i: /"; echo "$i-") echo foo >&3 echo bar >&3 exec 3>&- done then the result is not synchronous; it could be something like: 1: foo 1: bar 2: foo 2: bar 1- 3: foo 3:…
musiphil
  • 3,837
  • 2
  • 20
  • 26
5
votes
1 answer

Use process substitution - only send stderr to file

For some reason, it seems like stderr is being sent to stdout in the following bash script: exec > >( while read line; do echo " stdout: $line"; done ) exec 2> >( while read line; do echo " stderr: $line"; done ) echo "rolo" echo "cholo" >&2 if…
user5047085
1
2 3
8 9