I want merge (union) output from two different commands, and pipe them to a single command.
A silly example:
Commands I want to merge the output:
cat wordlist.txt
ls ~/folder/*
into:
wc -l
In this example, if wordlist.txt contains 5 lines and 3 files, I want wc -l
to return 8.
$cat wordlist.txt *[magical union thing]* ls ~/folder/* | wc -l
8
How do I do that?
Your magical union thing is a semicolon... and curly braces:
The curly braces are only grouping the commands together, so that the pipe sign
|
affects the combined output.You can also use parentheses
()
around a command group, which would execute the commands in a subshell. This has a subtle set of differences with curly braces, e.g. try the following out:You'll see that all environment variables, including the current working directory, are reset after exiting the parenthesis group, but not after exiting the curly-brace group.
As for the semicolon, alternatives include the
&&
and||
signs, which will conditionally execute the second command only if the first is successful or if not, respectively, e.g.Since
wc
accepts a file path as input, you can also use process substitution:This roughly equivalent to:
Mind that
ls ~/folder/*
also returns the contents of subdirectories if any (due to glob expansion). If you just want to list the contents of~/folder
, just usels ~/folder
.I was asking myself the same question, and ended up writing a short script.
magicalUnionThing
(I call itappend
):Make that script executable
Now you do
What it does:
$*
returns all arguments as a string. The output of that command goes to script standard output by default.So the stdout of magicalUnionThing will be its stdin + stdout of the command that is passed as argument.
There are of course simpler ways, as per other answers.
Maybe this alternative can be useful in some cases.