Your magical union thing is a semicolon... and curly braces:
{ cat wordlist.txt ; ls ~/folder/* ; } | wc -l
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:
cd $HOME/Desktop ; (cd $HOME ; pwd) ; pwd
cd $HOME/Desktop ; { cd $HOME ; pwd ; } ; pwd
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.
cd $HOME/project && make
ls $HOME/project || echo "Directory not found."
{ list; }
is a compound command. Frombash(1)
: list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. [..] The return status is the exit status of list. – Lekensteyn May 07 '12 at 19:18man bash
from the command line and browse it. – pablomme May 07 '12 at 19:24wordlist.txt
does not exist an error fromcat
will appear on standard error, but not on standard output, sowc -l
will not count any lines from it. Same goes for~/folder
not existing. One could add2> /dev/null
between each of these commands and their semicolon to prevent noise on standard error, but other than being ugly, error messages are harmless for the purpose of counting lines. – pablomme May 07 '12 at 19:39&&
and||
operators. The semicolon is appropriate in thecat ; ls
example though. – pablomme May 07 '12 at 20:26{ cat f.txt;ls -l;}|wc -l
, Syntax Error:{cat f.txt;ls -l;}|wc -l
– Rolf Mar 11 '18 at 11:25{
withbegin;
and replace}
withend
. So the example would be:begin; cat wordlist.txt ; ls ~/folder/* ; end | wc -l
– Aaron Feldman Nov 30 '18 at 02:43