Reputation: 6586
I was reading this question, in which someone is trying to get the number of lines in a file from the shell. From the comments, I saw and implemented that the following:
$ wc -l myfile.txt
30213 myfile.txt
$ wc -l < <filename>
30213
What I don't understand is, what the <
operator is doing here. Can someone explain why the <
operator chops off the file name from the output in this case?
Upvotes: 0
Views: 242
Reputation: 1446
In the first case, the filename
is supplied as an argument when calling wc
, this causes wc
to include it in the output.
In the second case, stdin is redirected from filename
, this makes wc
"unaware" of the file and thus not able to print its name. This is equivalent to calling cat filename | wc -l
.
So to answer your question, operator <
redirects input file descriptor to read from a given file. If descriptor number is not explicitly specified, then it defaults to standard input (fd 0). See here for a formal reference and here for a convenient description.
Upvotes: 2
Reputation: 53
wc
outputs the file name as part of the output when it reads from a file.
The <
redirects input and output between files/programs, following the direction of the "arrow". For more information on I/O redirection, this is a pretty handy link. The reason that the file name is "chopped off" is because wc
is technically no longer reading from a file.
Upvotes: 2