Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

e.g.

$ command < (command2 | grep pattern)
share|improve this question
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? –  Hauke Laging Apr 28 '13 at 8:49
    
@HaukeLaging No; the output of one pipeline as the input to one command. –  Thomas Apr 28 '13 at 9:13
    
How is this different from extending the pipeline? command2 | grep pattern | command –  Hauke Laging Apr 28 '13 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? –  Thomas Apr 28 '13 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 Apr 28 '13 at 9:42

2 Answers 2

up vote 2 down vote accepted

(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
share|improve this answer

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

$ command < $(command2 | grep pattern)
share|improve this answer
3  
Tried it? pastebin.com/686v9iQr –  manatwork Apr 28 '13 at 8:46
    
@manatwork Would you mind making your findings a formal answer so that this question can be successfully closed? –  Hauke Laging Apr 28 '13 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 Apr 28 '13 at 9:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.