bash: concatenating the output of multiple commands without using temp files
I keep running into the problem of trying to concatenate the output of multiple commands in order to pipe them to the next filter. It seems as though it must be trivial, but I cannot seem to find a solution.
Here's a real-world example: Code:
#!/bin/bash Code:
/bin/firstcommand | cat - $(/bin/secondcommand) | /bin/nextfilter Any suggestions? Thanks... --Ron |
This kind of task is accomplished by means of process substitution. In this case the output of a command is seen as a file, since process substitution uses a file descriptor. Example:
Code:
#!/bin/bash |
More generally, it's possible to use either a subshell or command grouping, and redirect the output of the whole group at once.
Code:
( command1 ; command2 ; command3 ) | cat Don't forget that the closing bracket in command grouping (and functions) must be separated from the contents by either a semicolon or a newline. This is because "}" is actually a command (keyword) of its own, and must be treated like one. |
Thank you, and thank you. Obvious now that I see it, but it has plagued me for years.
|
All times are GMT -5. The time now is 03:15 AM. |