4 ms·
Useless use of cat detected! Rememeber, nearly all cases where you have: cat file | some_command and its args ... you can rewrite it as: <file some_comm
by teddyh 7y ago
Useless use of cat detected!
Rememeber, nearly all cases where you have:
cat file | some_command and its args ...
you can rewrite it as:
<file some_command and its args ...
and in some cases, such as this one, you can move the filename to the arglist as in:
some_command and its args ... file
— Randal L. Schwartz (http://porkmail.org/era/unix/award.html#cat http://porkmail.org/era/unix/award.html#cat)
- robertelder 7y agohah, I knew someone would point that out (which is why I talked about it in the article). I actually prefer useless cat because when you're prototyping a pipeline it's very awkward to use non-useless cat. You'll probably start off with something like this to observe the content of the file: cat something.txt Using this doesn't work in bash: <something.txt Then, continuing with useless cat to build on it you do cat something.txt | grep stuff Which you can type easily from using 'up' in your terminal. But if you use non-useless cat you have to re-type the entire thing or move the cursor around: grep stuff < something.txt With useless cat, you can keep adding things and check the result: cat something.txt | grep stuff | sed 's/"//g' Or if you need to insert another filter before the last stage like this, you can just press "up" and insert it: cat something.txt | grep -v negmatch | grep stuff I don't think there is any easily-typed equivalent workflow with non-useless cat.
- jpxw 7y agoIf you’re working with a lot of data you probably want to pipe it into head anyway, initially, so <file head -n50 | whatever Can be the starting command. When you no longer need the head there, just get rid of “head |”. Although I agree that the pointing out of “useless cat” is usually not particularly useful or constructive.
- robertelder 7y agoUsing head when there's lots of data make sense, but I really don't see any advantage to avoiding useless cat. Useless cat is way faster to type and make additions to. I sort of get the feeling that 'useless cat' is really just a fun copypasta kinda like when people like post "I'd just like to interject for a moment. What you're referring to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it..."
- drran 7y agoIt's still good to be aware about `useless cat`, to save some CPU and I/O, when converting one liners into scripts.
- heliodor 7y agoRather than using head and worrying about the size of the file, it is easier to simply use "useless" cat then ctrl-c the stream of data that comes out.
- dblotsky 7y agoWhat makes it useless? It’s functionally the same but makes it easier to author pipelines, which I think is a valid use.
- teddyh 7y agocat foo | bar is useless use of cat, since it’s equivalent to < foo bar which is both shorter and starts one less process. Why do you think that “cat” makes it “easier to author pipelines”?
- dblotsky 7y ago1). It doesn’t work in all shells, so you have to think about your environment before you use it. 2). I often start with `bar < foo`, so if I need to add more arguments to `bar`, I need to always skip over the input. 3). If I just want to delete all processing and look at the input, I can’t just backspace away the processing because `< foo` is invalid.