Live data from Hacker News

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

abuseofcats.com

21–30 of 62 posts

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

#21
post #17

I raised eyebrows recently when I was working with someone and we needed to create a file and instead of starting an editor I did: cat > filename ... Ctrl-D

Why not touch or echo? No reason for an editor or cat

You can type the intended file contents as-is.

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

#22
post #19

Don't do this: cat file | wc -l => wc -l head -n 5 file cat file | awk '{print $1}' => awk '{print $1}' file cat file | sort => sort file Do this instead: cat file | wc -l => The front-cat abuse is all about the order. The effective solution needs to keep the relative order of arguments.

Or just use cat and spend your brainpower on interesting, useful, and/or worthwhile topics. It boggles my mind that anyone cares about this.

Probably, but knowing that redirection operators can be freely moved within normal arguments [EDIT: thank ButlerianJihad for pursuing me to make this more accurate] is useful.

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

#23
post #17

I raised eyebrows recently when I was working with someone and we needed to create a file and instead of starting an editor I did: cat > filename ... Ctrl-D

Why not touch or echo? No reason for an editor or cat

For a one line file sure, but I was creating multiple lines.

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

#24
post #19

Earlier quoted context omitted.

Or just use cat and spend your brainpower on interesting, useful, and/or worthwhile topics. It boggles my mind that anyone cares about this.

Probably, but knowing that redirection operators can be freely moved within normal arguments [EDIT: thank ButlerianJihad for pursuing me to make this more accurate] is useful.

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.

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

#25

Earlier quoted context omitted.

Probably, but knowing that redirection operators can be freely moved within normal arguments [EDIT: thank ButlerianJihad for pursuing me to make this more accurate] is useful.

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.

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

#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.

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

#27
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.

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.

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

#28
Unless you're executing these commands in a loop over a large number of items, or the item itself is gargantuan, it's almost always harmless.

Personally, when I'm exploring, I build a command line iteratively. Cat the file to see the content, pipe to grep to get the lines I want, sed/awk/cut/etc to finagle from there.

Post reply on HN