Hacker News new | ask | show | jobs
by Jakob 41 days ago
Very nice article. Refreshing to see one liners and fun projects. And the website’s glamour mode is great, too.

Although, as tradition requests, any example of the useless use of `cat` (UUOC) must be called out. The _much_ more efficient use would of course be

  tr '\n' ' ' < input.txt | tr -d '\r' | fold …
3 comments

These uses of cat aren't useless.

It improves readability. One reads shell commands from left to right, following the logic of each step in the pipeline individually. It makes sense to start with a file, then pipe that to command 1, pipe that to command 2, etc. It makes less sense to start with command 1 into which we put the file and then take the output of command 1 from before the file and put that into command 2 etc.

It also improves future editability. If you wanted to change the input to command 1, you go tweak the bit to the left of command 1 instead of having to hunt down where the < is stuck in the middle somewhere and potentially remove it and replace it with something off to the left anyway depending on whether it's a file or the output of some other command.

Using cat as the first step into a pipeline is almost always the right thing to do!

   <input.txt tr '\n' ' ' | tr -d '\r' | fold …
is valid bash and does the same as cat input.txt | tr ...
And now your mentor arrives and demands you change one character to source all input*.txt files
> tr '\n' ' ' < input.txt | tr -d '\r' | fold …

I enjoy discovering and decrypting these one liners much more than reading about what someone's AI agent generated recently.

How is avoiding a single invocation of cat much more efficient?

I feel if anything needs calling out, it's mistaking opinions for universal laws.

See https://news.ycombinator.com/item?id=23341711 if that really interests you.

Of course it doesn’t matter in in the grand scheme of things. We are talking about saving seconds in the most egregious example. But it’s nice to know one’s tools.