Created: 2016-09-19 — modified: 2016-09-21 — tags: bash
TL;DR: 3> >(command)
Process Substitution is nice, but rarely used Bash feature. It lets you put (in/out)put of one process in where another process expects a file.
For example (not real code):
ls | tee >(mail -s "listing" firstname.lastname@example.org)
lets you show output of
ls command on the screen and email it at the same time.
Also, it can be used with
exec >(tee ~/log)
to append all following script output to a file while printing it to the screen.
But what if you want to do it, while using a stream other then 1 (stdout)? Naive code like this
Will fail with a cryptic error message.
Instead, you must understand that Bash replaces the >(...) syntax with
file descriptor name in the command line
echo <(true) if you don't believe),
and that full syntax for working with custom streams looks like this:
N > file
(spaces are optional, N is the number of stream / file descriptor)
So what you actually want is written at the top of this article:
3 is the number of your stream,
> is redirection command,
then space to distinguish it from
>> "append" redirect,
>( begins the process substitution.