I was not aware of the award until today when some rookie tried to pin the UUOC on me for one of my answers. It was a cat file.txt | grep foo | cut ... | cut ...
. I gave him a piece of my mind, and only after doing so visited the link he gave me referring to the origins of the award and the practice of doing so. Further searching led me to this question. Somewhat unfortunately despite conscious consideration none of the answers included my rationale.
I did not meant to be defensive when educating him. After all, in my younger years I would have written the command as grep foo file.txt | cut ... | cut ...
because whenever you do the frequent single grep
s you learn the placement of the file argument and it is ready knowledge that the first is the pattern and the later ones are file names.
It was a conscious choice when I answered the question with the cat
prefix partly because of a reason of "good taste" (in the words of Linus Torvalds) but chiefly for a compelling reason of function.
The latter reason is more important so I will put it out first. When I offer a pipeline as a solution I expect it to be reusable. It is quite likely that a pipeline would be added at the end of or spliced into another pipeline. In that case having a file argument to grep screws up reusability, and quite possibly do so silently without an error message if the file argument exists. I. e. grep foo xyz | grep bar xyz | wc
will give you how many lines in xyz
contain bar
while you are expecting the number of lines that contain both foo
and bar
. Having to change arguments to a command in a pipeline before using it is prone to errors. Add to it the possibility of silent failures and it becomes a particularly insidious practice.
The former reason is not unimportant either since a lot of "good taste" merely is an intuitive subconscious rationale for things like the silent failures above that you cannot think of right at the moment when some person in need of education says "but isn't that cat useless".
However, I will try to also make conscious the former "good taste" reason I mentioned. That reason has to do with the orthogonal design spirit of Unix. grep
does not cut
and ls
does not grep
. Therefore at the very least grep foo file1 file2 file3
goes against the design spirit. The orthogonal way of doing it is cat file1 file2 file3 | grep foo
. Now, grep foo file1
is merely a special case of grep foo file1 file2 file3
, and if you do not treat it the same you are at least using up brain clock cycles trying to avoid the useless cat award.
That leads us to the argument that grep foo file1 file2 file3
is concatenating, and cat
concatenates so it is proper to cat file1 file2 file3
but because cat
is not concatenating in cat file1 | grep foo
therefore we are violating the spirit of both the cat
and the almighty Unix. Well, if that were the case then Unix would need a different command to read the output of one file and spit it to stdout (not paginate it or anything just a pure spit to stdout). So you would have the situation where you say cat file1 file2
or you say dog file1
and conscientiously remember to avoid cat file1
to avoid getting the award, while also avoiding dog file1 file2
since hopefully the design of dog
would throw an error if multiple files are specified.
Hopefully at this point you sympathize with the Unix designers for not including a separate command to spit a file to stdout, while also naming cat
for concatenate rather than giving it some other name. <edit>
there is such a dog, the unfortunate <
operator. It is unfortunate its placement at the end of the pipeline preventing easy composability. There is no syntactically or aesthetically clean way to place it at the beginning. It is also unfortunate in not being general enough so you start with the dog but simply add another filename if you also want it to be processed after the previous one. (The >
on the other hand is not half as bad. It has almost perfect placement at the end. It is typically not a reusable part of a pipeline, and accordingly it is distinguished symbolically.)</edit>
The next question is why is it important to have commands that merely spit a file or the concatenation of several files to stdout, without any further processing? One reason is to avoid having every single Unix command that operates on standard input to know how to parse at least one command line file argument and use it as input if it exists. The second reason is to avoid users having to remember: (a) where the filename arguments go; and (b) avoid the silent pipeline bug as mentioned above.
That brings us to why grep
does have the extra logic. The rationale is to allow user-fluency for commands that are used frequently and on a stand-alone basis (rather than as a pipeline). It is a slight compromise of orthogonality for a significant gain in usability. Not all commands should be designed this way and commands that are not frequently used should completely avoid the extra logic of file arguments (remember extra logic leads to unnecessary fragility (the possibility of a bug)). The exception is to allow file arguments like in the case of grep
. (by the way note that ls
has a completely different reason to not just accept but pretty much require file arguments)
Finally, what could have been done better is if such exceptional commands as grep
(but not necessarily ls
) generate an error if the standard input is available. This is reasonable because the commands include logic that violates the orthogonal spirit of the almighty Unix for user convenience. For further user convenience, i. e. for preventing the suffering caused by a silent failure, such commands should not hesitate to violate their own violation by alerting the user if there is a possibility of silent failure.
< file cmd1 args | cmd2 args ...
works too... so your argument of "left to right" is void. I often use it for clarity - the order I showed can cause people to pause, which isn't good. With higher thread counts becoming the norm, this is becoming less of an issue IMO...