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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 <(...
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 ...
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 ...
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)'...
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 ...
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. ...
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 ...
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)
...
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 ...
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]
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 ...
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 ...
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 ...
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 ...))...
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 ...
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 &...
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 &...
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;
...
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....
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 ...
Community wiki
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
process-substitution × 188bash × 127
pipe × 37
shell × 32
io-redirection × 27
shell-script × 23
file-descriptors × 14
zsh × 11
tee × 10
linux × 9
fifo × 9
subshell × 8
ssh × 7
command-line × 7
variable × 6
files × 5
sudo × 5
background-process × 5
posix × 4
macos × 4
diff × 4
grep × 3
find × 3
vim × 3
docker × 3