What is the correct way to redirect the output of multiple commands as input for another command?
e.g.
$ command < (command2 | grep pattern)
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this community(This refers to bash in case of doubt)
The output of one command or one pipeline can be made the input of another command by creating a (or extending the existing) pipeline:
command1 | command2
command1 | command2 | command3
Several commands (including pipelines) can be combined with a subshell or a list (group command). This combination becomes the first part of the pipeline then:
(command1; command2 | command3; command4) | command5
{command1; command2 | command3; command4;} | command5 # note the ; before }
Another possibility is a "here string" (or even a "here document"):
command2 <<< $(command1)
command2 <<EOT
First input line
$(command1)
Last input line
EOT
Other cases
Command substitution is used when the output shall be part of a command line i.e. if one command shall see the output of another as its own parameter:
echo $(date)
echo sees the output of date
as its parameter; as if it had been typed in the command line.
Process substitution makes the output of another process appearing as the content of a (non-seekable) file given as parameter on the command line.
grep bar <(echo $'foo\nbar\nbaz')
looks to grep like
echo $'foo\nbar\nbaz' > /path/to/file
grep bar /path/to/file
You have to execute the commands using the command substitution syntax.
$ command < $(command2 | grep pattern)
command2 | grep pattern | command
<( .... )
syntax is primarily used in those cases where a simple pipeline does not work, either because some program refuses to read from standard input and requires a file argument, or because there are several commands whose output should be fed into one other command, i.e.,cmd <(cmd1) <(cmd2)
.