Live data from Hacker News

PSA about abuse of cat(1) command. Don't abuse cats

abuseofcats.com

31–40 of 62 posts

Re: PSA about abuse of cat(1) command. Don't abuse cats

#31
post #25

Earlier quoted context omitted.

They are actually not “order-independent”, and their L-R parsing/processing is why constructs such as cat file > /dev/null 2>&1 work as intended.

funny enough, 2>&1 >/dev/null cat file appears to yield the same output. So i wonder where the not "order-independent" chimes in.

All of this depends on your specific shell and its parser. Fish doesn't let you put redirections at the beginning like that (though I wish it did), while GNU Bash does.

Re: PSA about abuse of cat(1) command. Don't abuse cats

#32
post #26
post #20

if this wanton abuse of cat(1) doesn't stop, we're on track to run out of PIDs by 2031! Just because Unix makes it cheap and easy to fork doesn't mean you have to! (who gives even a single shit, my god)

I like piping the output of cat and the mental image of one process feeding another. It's inconsequential, but it brings an epsilon of joy.

same I just like monads lol. cat + pipe feels purer and has lower mental load for me, which dominates the efficiency of spawning an extra process for, typically, a few microseconds.

Re: PSA about abuse of cat(1) command. Don't abuse cats

#33
The beauty of cat is that streams are the universal interface.

Program A might accept a file as the last positional arg. Program B might accept it as a named arg, where the name/flag could be anything from --input or -f or --file etc.

But a program will read from STDIN, which all good unix programs do, then piping cat into it works every time. I can write the cat foo.txt part before I even know what command I'm piping it into.

Re: PSA about abuse of cat(1) command. Don't abuse cats

#34
post #25

Earlier quoted context omitted.

funny enough, 2>&1 >/dev/null cat file appears to yield the same output. So i wonder where the not "order-independent" chimes in.

All of this depends on your specific shell and its parser. Fish doesn't let you put redirections at the beginning like that (though I wish it did), while GNU Bash does.

fish is not POSIX-compatible, and not Bourne-compatible, so I don't see how that really matters at all. I used the rc shell from plan9 for quite a while, and I wouldn't expect its syntax rules to match, either!

Re: PSA about abuse of cat(1) command. Don't abuse cats

#35
post #25

Earlier quoted context omitted.

They are actually not “order-independent”, and their L-R parsing/processing is why constructs such as cat file > /dev/null 2>&1 work as intended.

funny enough, 2>&1 >/dev/null cat file appears to yield the same output. So i wonder where the not "order-independent" chimes in.

`2>&1` redirects FD2 to the current contents of FD1 (stdout), then `> /dev/null` redirects FD1 to /dev/null. That results in your errors going into stdout, and discarding regular output altogether:

    0: stdin  -> stdin  -> stdin
    1: stdout -> stdout -> /dev/null
    2: stderr -> stdout -> stdout
When you flip the order, `> /dev/null 2>&1` moves FD1 to /dev/null first, and then FD2 to the contents FD1 (/dev/null again), so you discard both errors and standard output:

    0: stdin  -> stdin     -> stdin
    1: stdout -> /dev/null -> /dev/null
    2: stderr -> stderr    -> /dev/null
In your example, `cat file` is unlikely to produce any errors, which is why you're not seeing a difference.

Re: PSA about abuse of cat(1) command. Don't abuse cats

#36
post #33

The beauty of cat is that streams are the universal interface. Program A might accept a file as the last positional arg. Program B might accept it as a named arg, where the name/flag could be anything from --input or -f or --file etc. But a program will read from STDIN, which all good unix programs do, then piping cat into it works every time. I can write the cat foo.txt part before I even know what command I'm pipin…

This. Sometimes I want to see what I'm looking at and then (using that dump as a reference) follow up with a corresponding filter (| jq .key, or | tail -n 30). Sure, I could use less, but then I context switch on exit; no support from the scrollback buffer.

I've probably lost 10ms * 1E5 of my life from the extra PID. But, probably would lose more in the context switch.

Re: PSA about abuse of cat(1) command. Don't abuse cats

#38
post #25

Earlier quoted context omitted.

funny enough, 2>&1 >/dev/null cat file appears to yield the same output. So i wonder where the not "order-independent" chimes in.

You're absolutely wrong! It does not yield the "same output", and here is why: if you cause your command to actually produce output on stderr (fd 2) it will appear as terminal output, because you have actually succeeded in "redirecting" stderr to wherever stdout (fd 1) was pointing initially.

[deleted]

Re: PSA about abuse of cat(1) command. Don't abuse cats

#39
post #20

if this wanton abuse of cat(1) doesn't stop, we're on track to run out of PIDs by 2031! Just because Unix makes it cheap and easy to fork doesn't mean you have to! (who gives even a single shit, my god)

Is this a dig at IPv6?

Everything reminds me of her (IPv6) T_T

Edit: for context my home router is a TP Link and for some reason it has IPv6 disabled completely and I'm too scared to enable it.

Re: PSA about abuse of cat(1) command. Don't abuse cats

#40
post #33

The beauty of cat is that streams are the universal interface. Program A might accept a file as the last positional arg. Program B might accept it as a named arg, where the name/flag could be anything from --input or -f or --file etc. But a program will read from STDIN, which all good unix programs do, then piping cat into it works every time. I can write the cat foo.txt part before I even know what command I'm pipin…

Yeah, I read TFA, and my eyes were rolling the whole time. Some people really have a bee in their bonnet that "cat" is named that way because it was originally for concatenating files. Nobody fucking cares. It's the standard way for writing a file to standard out, and the general pattern of "cat file.txt | somecommand | othercommand | anothercommand" is so useful because it follows the pipeline pattern so well - read from standard in, write to standard out - that is the cornerstone of Unix shell commands IMO.

"Oh no, it spawns another process!!" Again, nobody cares.

Post reply on HN