Live data from Hacker News

The useful use of cat

mrmr.io

61–70 of 113 posts

Re: The useful use of cat

#62

if you prefer to use cat just to read your pipeline from left to right, you can just start your command with file redirection. It doesn't have to be at the end of the command. this works in bash too, but if you're using zsh, there're a couple of nice shortcuts on it's own works as more file and > file ... ... ^D allows you to put something into the file quickly without firing up the editor. Though > file will give yo…

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

The problem is that according to this argument `cat` should never be used for the thing it was designed for, because if you use it with more than one argument, it would give most Unix users pause, but if you saw it with only one argument they would understand it instantly.

The whole point of the "never use cat" Unix advice is a war between instrumentalism and design purism. Should things be known mostly for what they're useful for, or mostly for what they were made to do? If you understand this, then the war is not soluble but you can at least phrase a third position that will reconcile both sides. If you don't understand this, then the war is over an issue that doesn't even exist.

Hot take: ` | abc` and `abc | ` should both make sense because a file should be understood by default as a command that reads from and writes to a particular destination, and the shell should take it seriously that `abc | ` needs to be easily undoable if the file wasn't chmodded +x appropriately.

Re: The useful use of cat

#63
post #62

Earlier quoted context omitted.

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

The problem is that according to this argument `cat` should never be used for the thing it was designed for, because if you use it with more than one argument, it would give most Unix users pause, but if you saw it with only one argument they would understand it instantly. The whole point of the "never use cat" Unix advice is a war between instrumentalism and design purism. Should things be known mostly for what they…

[deleted]

Re: The useful use of cat

#64
post #57

cat | sudo tee somefile > /dev/null is still my favorite way to paste text into an ssh session and save it to a protected file.

The cat command can be omitted there, as tee reads from standard input by default, even if stdin points to a terminal. I was going to comment an actually useful (and unavoidable in bash) use of cat and ssh, which is to do do nothing with standard input and redirect it to a file: file' And you could just use scp, but I've found clients without scp and servers with the SFTP subsystem disabled.

I want do paste from my clipboard, not copy another file.

Re: The useful use of cat

#65
post #62

Earlier quoted context omitted.

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

The problem is that according to this argument `cat` should never be used for the thing it was designed for, because if you use it with more than one argument, it would give most Unix users pause, but if you saw it with only one argument they would understand it instantly. The whole point of the "never use cat" Unix advice is a war between instrumentalism and design purism. Should things be known mostly for what they…

What if you want to grep a bash script file?

Re: The useful use of cat

#66
post #7

Using `cat file.txt | ...` has always felt more natural to me, so I still use it. If I need to build a long command I've been using the excellent `up` tool to do it, e.g. `cat file.txt | up` https://github.com/akavel/up

Wow that looks amazing, thanks for telling me about this!

Indeed. A good addition to fzf and bat

Re: The useful use of cat

#67

if you prefer to use cat just to read your pipeline from left to right, you can just start your command with file redirection. It doesn't have to be at the end of the command. this works in bash too, but if you're using zsh, there're a couple of nice shortcuts on it's own works as more file and > file ... ... ^D allows you to put something into the file quickly without firing up the editor. Though > file will give yo…

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

Both of those invocations would give me pause, because grep is a command that can handle file arguments on its own; in fact it can incorporate knowledge of those filenames into its output quite usefully, so I would generally avoid redirects into grep when I could use it to explicitly, directly, open the files in question.

Re: The useful use of cat

#68

if you prefer to use cat just to read your pipeline from left to right, you can just start your command with file redirection. It doesn't have to be at the end of the command. this works in bash too, but if you're using zsh, there're a couple of nice shortcuts on it's own works as more file and > file ... ... ^D allows you to put something into the file quickly without firing up the editor. Though > file will give yo…

It’s shell specific though, just like using FOO=bar cmd prefixing to make environment changes. Better to use env(1) and keep everything rooted in Unix processes rather than shell syntax, imho.

  # bashisms
  

Re: The useful use of cat

#69
post #9

Does anyone take the "don't use cat" stuff seriously?

It’s a hump curve with virtue signalling in the middle, like a thousand other things in life.

Noob+naive: use cat because don’t know better.

Experienced+smart ass: use shell direction to distinguish oneself from noobs.

Older+wiser: use cat again because it flows left to right, isn’t bash specific like “…but mostly because it doesn’t make one come across as a nit picking asshole to the expensive noobs you’ve just spent months trying to hire.

Re: The useful use of cat

#70

if you prefer to use cat just to read your pipeline from left to right, you can just start your command with file redirection. It doesn't have to be at the end of the command. this works in bash too, but if you're using zsh, there're a couple of nice shortcuts on it's own works as more file and > file ... ... ^D allows you to put something into the file quickly without firing up the editor. Though > file will give yo…

If I saw It would give me pause. If I saw cat file | grep ... I would understand it instantly, as would any other Unix user. Therefore the latter is better code.

Better code, yeah I agree. It's way more easily understandable. At least for me though, 99% of my bash use is stuff that will never be seen by anyone else, so the simpler keystrokes one may prove useful. Maybe if I get used to it :)
Post reply on HN