5

What is the correct way to redirect the output of multiple commands as input for another command?

e.g.

$ command < (command2 | grep pattern)
5
  • 3
    Do you really mean the output of multiple commands ot the output of one pipeline? And which shell are we talking about, or are you looking for the perfectly portable solution? Commented Apr 28, 2013 at 8:49
  • @HaukeLaging No; the output of one pipeline as the input to one command.
    – underskor
    Commented Apr 28, 2013 at 9:13
  • How is this different from extending the pipeline? command2 | grep pattern | command Commented Apr 28, 2013 at 9:17
  • @HaukeLaging Ah, I didn't know they were the same! Thank you. Would you like to answer so I can mark it as accepted?
    – underskor
    Commented Apr 28, 2013 at 9:28
  • The <( .... ) 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).
    – Uwe
    Commented Apr 28, 2013 at 9:42

2 Answers 2

5

(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
-2

You have to execute the commands using the command substitution syntax.

$ command < $(command2 | grep pattern)
3
  • 3
    Tried it? pastebin.com/686v9iQr
    – manatwork
    Commented Apr 28, 2013 at 8:46
  • @manatwork Would you mind making your findings a formal answer so that this question can be successfully closed? Commented Apr 28, 2013 at 8:54
  • @HaukeLaging, I prefer to let the answer's owner to make the necessary corrections, instead of posting a concurrent answer. After all, Spack seems to be new here and this answer's age is less than half hour.
    – manatwork
    Commented Apr 28, 2013 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .