Live data from Hacker News

How efficient can cat(1) be?

ariadne.space

41–50 of 51 posts

Re: How efficient can cat(1) be?

#41
post #39

From my experience, it will never be so efficient that someone smarter than me doesn't publicly shame me for winning the "useless use of cat" award on a forum where I ask for help. 28 years later and I'm still sore I asked for help as a 15 year old that one time. Very effective way to teach a new user.

Another reason I still use "cat" is that I don't want the remaining commands to modify the input file, but I don't want to spend the time to inspect the command line to make sure that is the case. For example I'm pretty sure "grep" won't change the input file but "sed" may depending on the "-i" flag. Using "cat" conveniently bypasses all that thinking because the program only gets stdin from a pipe.

You can replace `cat file | cmd` with just `cmd Even when trying to say something like `echo $(cat foo)`, in Bash you can write `echo $(<foo)`. Though other shells may still require `cat` in this scenario.

Re: How efficient can cat(1) be?

#43
post #39

Earlier quoted context omitted.

Another reason I still use "cat" is that I don't want the remaining commands to modify the input file, but I don't want to spend the time to inspect the command line to make sure that is the case. For example I'm pretty sure "grep" won't change the input file but "sed" may depending on the "-i" flag. Using "cat" conveniently bypasses all that thinking because the program only gets stdin from a pipe.

You can replace `cat file | cmd` with just `cmd Even when trying to say something like `echo $(cat foo)`, in Bash you can write `echo $(<foo)`. Though other shells may still require `cat` in this scenario.

Or <file cmd, to preserve the order.

Re: How efficient can cat(1) be?

#44

Earlier quoted context omitted.

that’s a lesson that’s no longer applicable on modern machines I think? —- it often makes sense to start a sequence of piped commands with a cat invocation. One reason is that then the order of sources and sinks in the command syntax matches reality.

I always pipe from cat, as a matter of habit. Reason being: I've probably just catted the file at least once, and it's even likely I catted it right before piping it. So I can extend that command from the history, and if I keep piping (likely) I don't have the weird syntactic stutter at the beginning where `blah Also, you can't mistype `cat file.txt | blah` and overwrite file.txt accidentally with the output of blah.…

‘<file.txt blah’ works just fine.

Re: How efficient can cat(1) be?

#45

Earlier quoted context omitted.

I always pipe from cat, as a matter of habit. Reason being: I've probably just catted the file at least once, and it's even likely I catted it right before piping it. So I can extend that command from the history, and if I keep piping (likely) I don't have the weird syntactic stutter at the beginning where `blah Also, you can't mistype `cat file.txt | blah` and overwrite file.txt accidentally with the output of blah.…

‘<file.txt blah’ works just fine.

It's preference, like Python list comprehensions vs Ruby method chain.

Re: How efficient can cat(1) be?

#46

Earlier quoted context omitted.

I always pipe from cat, as a matter of habit. Reason being: I've probably just catted the file at least once, and it's even likely I catted it right before piping it. So I can extend that command from the history, and if I keep piping (likely) I don't have the weird syntactic stutter at the beginning where `blah Also, you can't mistype `cat file.txt | blah` and overwrite file.txt accidentally with the output of blah.…

‘<file.txt blah’ works just fine.

I often use cat because when constructing a pipeline I'll do multiple rounds of:

    head filename | ...
Once its working I find it easy to just type C-aM-dcat (i.e. beginning-of-line kill-word cat).

Re: How efficient can cat(1) be?

#47

Earlier quoted context omitted.

You can replace `cat file | cmd` with just `cmd Even when trying to say something like `echo $(cat foo)`, in Bash you can write `echo $(<foo)`. Though other shells may still require `cat` in this scenario.

Or <file cmd, to preserve the order.

That works in Bash, it doesn't work in all other shells though (it doesn't work in Fish at least, I haven't checked others).

Re: How efficient can cat(1) be?

#48

Earlier quoted context omitted.

I always pipe from cat, as a matter of habit. Reason being: I've probably just catted the file at least once, and it's even likely I catted it right before piping it. So I can extend that command from the history, and if I keep piping (likely) I don't have the weird syntactic stutter at the beginning where `blah Also, you can't mistype `cat file.txt | blah` and overwrite file.txt accidentally with the output of blah.…

‘<file.txt blah’ works just fine.

Thanks for the reminder (sincerely) but to me this is worse, I would use a shell which doesn't offer this particular form of irregular grammar, I hate it.

100% aesthetic, I shouldn't be able to point a file at my prompt and have it end up streamed into the next word. Just awful, 0/10, do not want.

Re: How efficient can cat(1) be?

#49

Earlier quoted context omitted.

‘<file.txt blah’ works just fine.

Thanks for the reminder (sincerely) but to me this is worse, I would use a shell which doesn't offer this particular form of irregular grammar, I hate it. 100% aesthetic, I shouldn't be able to point a file at my prompt and have it end up streamed into the next word. Just awful, 0/10, do not want.

From another perspective it’s more regular that position doesn’t matter. (This behavior works in all POSIX shells, zsh, rc, etc. But not fish :p)

Re: How efficient can cat(1) be?

#50

Earlier quoted context omitted.

Or <file cmd, to preserve the order.

That works in Bash, it doesn't work in all other shells though (it doesn't work in Fish at least, I haven't checked others).

I think fish is the outlier here, not the norm. It’s specified in POSIX and most non-POSIX shells like zsh and rc support it.
Post reply on HN