On the useful use of cat

Some people insist that we should never use a cat.

This is at best a subjective opinion, and at worst harmful bullying.

Shell pipelines are not things of static beauty, they are interactive dialogs with the UNIX shell, chatting with it to come up ad-hoc ways of gluing together tools to the get the at the bespoke outcomes we want.

Beginning a shell pipe with a `cat` provides us maximal flexibility during the exploratory phase. Here is a common pattern I follow:

cat some-huge-file | head | ...

I can fill in the `...` part through trial and error, without running it on the entire input. A la test driven development.

Then, when the rest of the command feels complete, I splice out the head from the pipeline using the key combinations Ctrl - P (prev command), Ctrl - A (beginning of line), Alt/Opt - F twice (to skip over the cat) and Alt/Opt - D twice. Seems a lot when I spell it out, but my hands do it instantly without me thinking.

The exact key combinations are not important, and it is perhaps confusing to bring them into the discussion. But I wanted to show how I think of the shell pipe concretely - as a series of steps, with the first step being an input and the last step being an output - and in such a formulation, placing the head after the cat comes totally naturally.


The cat | head is redundant, and the following would seem equivalent:

head some-huge-file | ...

which later on, once the pipe has been constructed, can be modified to

cat some-huge-file | ...

leaving the rest unchanged. This is fine really, achieves conceptual equivalence with the “first step as the command-agnostic input” way of thinking I'm describing, and is shorter to boot.

Why don't I do it that way (`head` instead of `cat`)? Because for me it'd only be a local maxima.

For some commands, it would indeed be the best. But I don't always construct commands using cat | head. Sometimes I subconsciously know how things are going to go, by virtue of having done similar things previously, and while I might not necessarily construct the correct shell incantation in the first go, I might too, and usually if there is any mistake it'll take me one or two minor tweaks to get it right.

In such cases, my hands don't bother with the `head`. So for me, always beginning the pipeline with a cat is a global maxima. I don't need to put any conscious thought into deciding if the shell pipeline is going to be complicated or not, and if I choose incorrectly I can still easily transition between the two modes.


It is fine if someone decides not to use cat ever, it is just a personal preference. But what I disike are the people who go around giving the “useless use of cat award” to insist that others also stop using it. I don't know if these folks are just misguided, or are intentionally being an ass.