Useless Use of Cat Award
Useless Use of Cat Award
If you've been reading<br>comp.unix.shell<br>or any of the related groups<br>(comp.unix.questions<br>inter alia)<br>for any amount of time, this should be a familiar topic.
I made this web page on the topic primarily so I'd have a simpler URL<br>than one of those ghastly Deja News searches to hand to people. I've<br>tried to reconstruct Randal's standard form letter from looking at his<br>postings<br>(see end)<br>and added some comments of my own.
If you came here looking for material about abuse of feline animals, try<br>this Alta Vista search<br>instead.
Contents:
Useless Use of<br>cat<br>Useless Use of<br>kill -9<br>Useless Use of<br>echo<br>Useless Use of<br>ls *<br>Useless Use of<br>wc -l<br>Useless Use of<br>grep | awk<br>Useless Use of<br>Backticks<br>Useless Use of<br>test
Assorted Other Gripes
Related Resources
Reconstructions of Randal's Form Letters
The Useless Use of Cat Award
The venerable<br>Randal L. Schwartz<br>hands out Useless Use of Cat Awards from time to time;<br>you can see some recent examples in<br>Deja News.<br>(The subject line really says "This Week's Useless Use of Cat Award"<br>although the postings are a lot less frequent than that nowadays).<br>The actual award text is basically the same each time, and the ensuing<br>discussion is usually just as uninteresting, but there are some refreshing<br>threads there among all the flogging of this dead horse.
The oldest article Deja News finds is from 1995, but it's actually a<br>followup to an earlier article. By Internet standards, this is thus<br>an Ancient Tradition.
Exercise: Try to find statistically significant differences between<br>the followups from 1995 and the ones being posted today.
(See below for a<br>reconstruction of the Award text.)
Briefly, here's the collected wisdom on using cat:
The purpose of cat is to concatenate (or "catenate")<br>files. If it's only one file, concatenating it with<br>nothing at all is a waste of time, and costs you a process.
The fact that the same thread ("but but but, I think it's cleaner /<br>nicer / not that much of a waste / my privelege to waste processes!")<br>springs up virtually every time the Award is posted is also Ancient<br>Usenet Tradition.
Of course, as Heiner points out, using cat on a single<br>file to view it from the command line is a valid use of cat<br>(but you might be better off if you get accustomed to using less<br>for this instead).
In a recent thread on comp.unix.shell, the following<br>example was posted by Andreas Schwab as another Useful Use of Cat<br>on a lone file:
{ foo; bar; cat mumble; baz } | whatever
Here, the contents of the file mumble are output<br>to stdout after the output from the programs foo and<br>bar, and before the output of baz. All<br>the generated output is piped to the program whatever.<br>(Read up on shell programming constructs if this was news to<br>you :-)
Other Fun Awards
This could evolve into a good listing of "don't do that" shell<br>programming idioms.
Useless Use of Kill -9
Randal also posts his<br>Useless Use of Kill -9 Award<br>although much less frequently.
(See below for a<br>reconstruction of the Award text.<br>It explains the issues clearly enough.)
Useless Use of echo
This is really a special case of<br>Useless Use of Backticks<br>but it deserves its own section because it's something you see<br>fairly frequently.
The canonical form of this is something like
variable="something here, or perhaps even the result of backticks"<br>some command -options `echo $variable`
Depending a little bit on what exactly you have the variable for,<br>this can be reduced at least to
variable="something here, or perhaps even the result of backticks"<br>some command -options $variable
and there is often no real reason to even think of using echo in backticks<br>when the simpler construct will do.
(There is a twist: echo will "flatten" any whitespace in<br>$variable into a single space -- unless you double-quote<br>$variable, of course --, and sometimes you can legitimately<br>use echo in backticks for this side effect. But that's rarely necessary or<br>useful, and so most often, this is just a misguided use of echo.)
There is another example in the next section, and a longer rant about<br>Useless Use of Backticks<br>further down the page. There is also a parallel, slightly different<br>example on the<br>Backticks Example page
Useless Use of ls *
Very clever. Usually this is seen as part of a for loop:
for f in `ls *`; do<br>command "$f" # newbies will often forget the quotes, too<br>done
Of course, the ls is not very useful. It will just waste<br>an extra process doing absolutely nothing. The * glob<br>will be expanded by the shell before ls even gets to<br>see the file names (never mind that ls lists all files by<br>default anyway, so naming the files you want listed is redundant here).
Here's a related but slightly more benign error<br>(because echo is often built into the shell):
for f in `echo *`; do<br>command "$f"<br>done
But of course the backticks are still useless, the glob itself already<br>does the expansion of the file names. (See<br>Useless Use of echo<br>above.) What was meant here was...