I was just trying to search through a log file. Innocently, I typed:
cat logs.txt | grep ERROR
It worked. I was happy.
Then, during a code review (yes, we review bash scripts too), someone yelled — okay, maybe not literally, but they left a fiery comment:
“Useless use of cat. Don’t do this!”
I blinked. What? What did cat
ever do to you?
The Problem
Apparently, what I had just written is called UUOC — Useless Use of Cat. And while the command did technically work, it was considered inefficient and unnecessary.
What I should have written was:
grep ERROR logs.txt
Same result, but cleaner, simpler, and slightly more efficient. Oops.
The Solution
After recovering from the mild bash-shaming, I learned that cat
is often misused. It’s useful when you’re chaining multiple files or using process substitution, but when you’re piping a single file into another command like grep
, awk
, or sed
, you’re just adding an unnecessary step.
I updated the script, committed the fix, and promised to be nicer to shell syntax going forward.
Deeper Explanation
So why is cat
frowned upon in this case?
When you use cat filename | grep something
, you’re:
- Creating an extra process (
cat
) - Piping output unnecessarily
- Violating the Unix philosophy of doing one thing well — directly
It may not slow your system down much for small files, but in larger scripts or cron jobs, these small inefficiencies add up.
Even tools like ShellCheck will warn you about this exact pattern.
Best Practices
- Don’t use
cat
when the following command can read the file directly (likegrep
,awk
,sed
,head
, etc.) - Use
cat
when:- You’re concatenating multiple files
- You’re using process substitution or complex pipelines
- It actually adds clarity (rare, but okay)
- Run your scripts through
shellcheck
for a free sanity check
Conclusion
So yes, I used cat | grep
, and someone yelled at me. But I learned something.
Bash is quirky, powerful, and weirdly opinionated. If it works, great — but if it can be cleaner, take the feedback. Even if it’s just about a poor cat.
Leave a Reply