Hacker News new | ask | show | jobs
by dagenix 2936 days ago
This is one of my pet peeves - complaining about technically unnecessary, but fully benign uses of cat.

Yes, 'cat FILENAME | blush "some text"' and 'blush "some text" < FILENAME' do the same thing. But, what if you don't have permission to read the file - the former be re-written as 'sudo cat FILENAME | blush "some text"' - the latter form can't. What if you want to build a pipeline? I think its pretty persuasive that 'cat FILENAME | blush "some text" | sort' reads better than 'blush "some text" < FILENAME | sort' - the former reads from left-to-right, the latter reads from the middle, to the left, and then bounces over to the right. Tastes may very - but, I think its a hard sell that such an opinion is clearly wrong.

So, yes, its unnecessary. And, yes, in a script using cat like that can complicate error handling. But, for interactive use, what advice exactly are you trying to convey?

3 comments

Sometimes things are lost over text -- my comment was meant to be more whimsical than it actually read. Just the old http://porkmail.org/era/unix/award.html joke.

My actual (light) advice is merely that we're all guilty of inappropriately using IO redirection facilities that punish the performance of our shells. `cat <filename> | grep <expression>` should be replaced by `grep <expression> <filename>`.

No doubt that pipelines are easier to read. The author has a whole section in README demonstrating blush's ability to read STDIN - nothing is lost by using best practices everywhere else. Documentation matters, and it should communicate best practices.

A solution that I personally prefer, because the common form looks weird for me:

< FILENAME blush "some text"

I've found that unnecessary use of cat can sometimes have a noticeable performance impact, and I suspect that on the right system with the right filesizes this could be one of those circumstances. (Obviously it doesn't matter one whit with small files.)