What is the difference between the statement cat a.txt | wc
and the statement wc < cat a.txt
. In both cases, isn’t the output of cat a.txt
being directed into wc
?

- 11
1 Answers
Absolutely not. In your second case (wc < cat a.txt
) you are invoking the command wc a.txt
while connecting a file named cat
to the standard input of the process.
It might seem confusing, but most shells allow you to redirect the input anywhere in the command line.
wc a.txt < cat
would be the same as (the arguably more confusing)
wc < cat a.txt
To redirect the output of a command to the input of another, you use the pipe character. To invoke a command with a file as its standard input, you use the chevrons.
Now, modern shells will let you type commands like this one:
wc <(cat a.txt)
This is called process substitution and is not exactly the same thing as either of the two methods you were asking about. In this case, the shell will invoke the process cat a.txt
and "catch" its output in a file descriptor. Then, the shell will invoke the "main" command (wc) and pass a reference to that file descriptor as an argument, as if it were a file name. It enables a command that expects a simple file name to read the output of an ad hoc command.

- 3,821
- 1
- 34
- 42
-
What about `wc < a.txt`? Is it the same as `cat a.txt | wc`? – Elf Aug 31 '19 at 03:56
-
Functionally, it is. It will achieve the same result, but `cat` is useful really only when you need to con**cat**enate files (e.g. `cat a.txt b.txt c.txt d.txt | wc`). `cat one_file | command` spawns cat for no reason, while you could have simply done `command < one_file`. – sleblanc Aug 31 '19 at 04:43
-
The duplicate has a good answer by Jonathan Leffler which shows just how much extra overhead a useless `cat` adds. – tripleee Aug 31 '19 at 04:48
-
@tripleee, ah... – sleblanc Aug 31 '19 at 04:52
-
I am utterly confident that OP got more out of this answer than they could out of the first answer in the question you linked. 11 paragraphs of someone rambling on how they prefer their arguments to be clearer and more explicit… – sleblanc Aug 31 '19 at 05:24
-
1Indeed, and I have upvoted your answer; but I don't think this particular question is going ho add value for future visitors. – tripleee Aug 31 '19 at 05:28