Redirection is used to redirect stdout/stdin/stderr!
Ex: ls > log.txt.
Pipes are used to give the output of a command as input to another command.
Ex: ls | grep file.txt
Why exactly are these two operators doing the same thing?
Why not just write ls > grep
to pass the output through, isn't this just a kind of redirection also?
I realize Linux is "Do one thing and do it well", so there has to be more of a logical reason that I'm missing.
You do need a differentiating syntax feature - and using >
vs. |
will do just fine.
If you used >
in both scenarios, how would you know whether
ls > grep
is trying to write to a file named grep
or send input to the grep
command?
grep
is perhaps not the best example, as you may then be tempted to disambiguate by the presence of grep
's mandatory arguments; however, (optionally) argument-less commands do exist, such as column
.
that other guy offers another example in the comments: test
may refer to a test output file or to the argument-less invocation of the standard test
command.
Another way of looking at it:
Your suggestion is essentially to use >
as a generic send-output-somewhere operator, irrespective of the type of target (file vs. command).
However, that only shifts the need for disambiguation, and then you have to disambiguate when specifying the target - is it a file to output to or a command to run?
Given that the shell also has an implicit disambiguation feature when it comes to the first token of a simple command - foo [...]
only ever invokes a command - differentiating at the level of the operator - >
for outputting to files, |
for sending to commands - is the sensible choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With