Skip to main content
38 votes
Accepted

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

That feature was introduced by ksh (first documented in ksh86) and was making use of the /dev/fd/n feature (added independently in some BSDs and AT&T systems earlier).  In ksh and up to ksh93u, it ...
Stéphane Chazelas's user avatar
34 votes
Accepted

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

Yes, bash, when called as sh, runs in POSIX mode, disabling all its bash only features. From the manual - Invoked with name sh If Bash is invoked with the name sh, it tries to mimic the startup ...
Inian's user avatar
  • 12.9k
30 votes
Accepted

Process substitution in GNU Makefiles

/bin/sh may be bash on your system, but when invoked as sh, bash will be running in POSIX mode (as if POSIXLY_CORRECT was defined, or it was started with --posix). In this mode, process substitutions ...
Kusalananda's user avatar
  • 341k
27 votes
Accepted

The process substitution output is out of the order

Yes, in bash like in ksh (where the feature comes from), the processes inside the process substitution are not waited for (before running the next command in the script). for a <(...) one, that's ...
Stéphane Chazelas's user avatar
26 votes
Accepted

Difference between subshells and process substitution

A command substitution ($(...)) will be replaced by the output of the command, while a process substitution (<(...)) will be replaced by a filename from which the output of the command may be read. ...
Kusalananda's user avatar
  • 341k
22 votes
Accepted

Script not working when passed to bash via -c

In your one-liner, the $1 is within a double quoted string: "cat <(date|awk '{print $1}')" This means that it will be expanded by the interactive shell. If the first positional parameter, $1, is ...
Kusalananda's user avatar
  • 341k
18 votes
Accepted

bash wait for process in process substitution even if command is invalid

Yes, in bash like in ksh (where the feature comes from), the processes inside the process substitution are not waited for. for a <(...) one, that's usually fine as in: cmd1 <(cmd2) the shell ...
Stéphane Chazelas's user avatar
16 votes

How to emulate Process Substitution in Dash?

You can reproduce what the shell does under the hood by doing the plumbing manually. If your system has /dev/fd/NNN entries, you can use file descriptor shuffling: you can translate main_command <(...
Gilles 'SO- stop being evil''s user avatar
16 votes
Accepted

What is more efficient or recommended for reading output of a command into variables in Bash?

First note that using read without -r is to process input where \ is used to escape the field or line delimiters which is not the case of /etc/passwd. It's very rare that you would want to use read ...
Stéphane Chazelas's user avatar
14 votes
Accepted

Process substitution inside a subshell to set a variable

Those are the errors you get when trying to perform a process substitution in bash when the shell is running in POSIX mode. The bash shell does not support process substitutions in POSIX mode. bash ...
Kusalananda's user avatar
  • 341k
12 votes
Accepted

Specifying the file extension produced by zsh process substitution

Somewhere in the depths of zshparam(1) one may find: TMPSUFFIX A filename suffix which the shell will use for temporary files created by process substitutions (e.g., `=(list)'...
thrig's user avatar
  • 35.2k
11 votes
Accepted

Why is there a "/dev/fd/63" in the output of "echo 123 >(cat)"?

The process substitution >(thing) will be replaced by a file name. This file name corresponds to a file that is connected to the standard input of the thing inside the substitution. The following ...
Kusalananda's user avatar
  • 341k
11 votes
Accepted

Bash - process substitution in chroot without /dev and /proc

On Linux, /dev/fd is a symbolic link to /proc/self/fd, where /proc/self is a symlink to the process directory of the calling process. /proc/$pid/fd then contains magic links to the open filehandles. ...
ilkkachu's user avatar
  • 142k
10 votes

Sensitive data in process substitution

Let's break it down: content=$(cat -) That's the same as content=$(cat). That's using command substitution which in bash uses a pipe. At one end of the pipe, cat is writing what it reads from its ...
Stéphane Chazelas's user avatar
9 votes
Accepted

How to emulate Process Substitution in Dash?

The question in the current bounty notice: the general example is too complicated. Can somebody please explain how to implement following example? diff <(cat "$2" | xz -d) <(cat "$1" | xz -d) ...
fra-san's user avatar
  • 10.4k
9 votes

Process substitution and pipe

It should be noted that process substitution is not limited to the form <(command), which uses the output of command as a file. It can be in the form >(command) which feeds a file as the input ...
Weijun Zhou's user avatar
  • 3,458
9 votes

How to pass to ssh the key stored in a shell environment variable?

export MYKEY=`cat key.pem` ssh-add - <<< "$MYKEY" ssh [email protected]
ADV-IT's user avatar
  • 229
9 votes
Accepted

Difference between <(commands) and >(commands) in process substitution

You use: consumer <(feeder) For the output of feeder to be fed to consumer, when consumer cannot take its input from stdin (in which case you'd simply use feeder | cousumer) but only from file ...
Stéphane Chazelas's user avatar
8 votes
Accepted

Wait for Bash process substitution subshells

In bash, you can't wait for process substitution. In: cmd1 > >(cmd2) the whole command finish as soon as cmd1 finish, regardless the status of cmd2. You have to implement a mechanism to ...
cuonglm's user avatar
  • 155k
8 votes
Accepted

`tee` and `bash` process substitution order

In echo foo | tee >(rev) | (sleep 1; cat) In bash like in ksh, but unlike zsh, the stdout of rev also is the pipe to (sleep 1; cat). echo, tee, rev and the (...) subshell are started at the same ...
Stéphane Chazelas's user avatar
8 votes
Accepted

Misunderstanding the purpose of a process substitution

`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 ...))...
muru's user avatar
  • 75k
8 votes
Accepted

Process substitution and redirection using tee

Order of redirection is important because Bash applies them in the order it finds them on the command it interprets. This is on purpose so that you can have idioms like > file 2>&1 working ...
LL3's user avatar
  • 5,478
7 votes

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

Use a coprocess. Using the coproc builtin you can start a subprocess, read its output and check its exit status: coproc LS { ls existingdir; } LS_PID_=$LS_PID while IFS= read i; do echo "$i"; done &...
Feuermurmel's user avatar
7 votes

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

You can rely on heredoc and /dev/fd/n files to achieve that. For example, in bash you can do this: #!/usr/bin/env bash paste <(echo "$SHELL") <(echo "$TERM") <(echo &...
Fritjof Larsson's user avatar
7 votes

Equivalence to the usage of process substitution?

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; ...
Stephen Kitt's user avatar
7 votes

The process substitution output is out of the order

You can pipe the second command into another cat, which will wait until its input pipe closes. Ex: prompt$ echo one; echo two > >(cat) | cat; echo three; one two three prompt$ Short and simple....
Nicholas Pipitone's user avatar
7 votes
Accepted

How to use bash process substitution for a sftp private key file?

Any idea why sftp is unable to read the temporary file descriptor? Few facts: the <(pass foo) process substitution will cause bash to create a pipe, run the pass foo command asynchronously with ...
7 votes
Accepted

How does exec > work in AWS EC2 user data?

Let's break that in to pieces: exec applies redirections on the shell itself, so indeed exec > somefile would direct all later output from the shell and its children to that file. Here, instead of ...
ilkkachu's user avatar
  • 142k

Only top scored, non community-wiki answers of a minimum length are eligible