Redirecting Input and Output - wc

I am studying bash commands in the Web Development path. I am currently doing this project and point 4 reads like this:

Redirect plants.txt as standard input to the wordcount command. Use the -l option on the wordcount command.

I’d have done something like this (which doesn’t work):

cat plants.txt | wc -l

Instead, the suggested solution is this:

wc -l < plants.txt

Can anyone please help me understand why the suggested solution works, while mine doesn’t?

Thank you in advance.

Why the suggested solution works:

wc -l < plants.txt pushes the contents of plants.txt into wc -l (note the direction of the angle bracket). By default, the output of wc -l will run in the terminal, solving this exercise.

Why your solution doesn’t work:

I’m not sure. I ran a series of tests of both commands offline on the same file, and both had the same output (regardless of the contents of plants.txt). Without reading the project (which I do not have access to), I can’t say for sure, but I’d guess it’s Codecademy wanting something done in a particular way to demonstrate understanding of a concept it’s trying to teach.

The other thought I had was that the suggested solution possibly runs more efficiently than yours? Certainly it appears (at least to me) to be more readable.

1 Like

< is still quite unclear to me to be honest, it’s like… I thought of it just like an alternative to >, but probably I am not right there.

Thank you for your answer pal! =)

Take a look at this article. It pretty effectively explains angle brackets in bash (and a few of their use cases).

1 Like