Live data from Hacker News

The useful use of cat

mrmr.io

101–110 of 113 posts

Re: The useful use of cat

#101
post #92

Earlier quoted context omitted.

> Using `cat file.txt | ...` has always felt more natural to me Use whatever you prefer¹, but if what you care for is the logic of reading left to right, you can do so without `cat` by doing instead ` ¹ It’s absurd this needs to be spelled out, but here we are.

I don't care about fancy ' I want to type 'cat'. The visual imagery of summoning a cute cat to do my POSIX sorcery trumps all else.

> I want to type 'cat'.

Then do. I’m certainly not stopping you, or saying you’re wrong to do it. I offered an alternative with no judgement for the other method.

Re: The useful use of cat

#102

Earlier quoted context omitted.

My coworker said the same thing. But now that you know, will it give you pause? Shell has tricky and arcane corners that are best to avoid, but you still do better to know about them, lest they bite you (shellcheck helps). I don't think that "I/O redirection can precede the command name" is particularly tricky or arcane. What bothers me is that it doesn't work with loops: The shell's grammar is quite a thing.

“Subsetting” a language is a useful practice. Consider the set of language features someone has to know in order to understand your code. If you can make that set smaller (without unreasonably sacrificing functionality), you lower the barrier to entry. More people can read your code. The typical retort I hear is “but it’s just this one feature, how hard is it to learn just one feature?” But you’re only considering yo…

You make good points. The only part I disagree with is "forcing your readers to learn more shell syntax." Who am I to say what the reader does or doesn't know about the shell? I don't even know what I don't know about the shell. The language is a minefield. I always have to look up the rules for how prefix/suffix substitution works, or the finer points of heredocs, or just when exactly something should be quoted, or the precedence of boolean operators, or the difference between "[" and "test", or ...

Yes, let's be considerate and not use these features. But then we must agree on the specifics of the subset. If we're going to do that, then let's just learn the language proper instead. Or, avoid shell scripts entirely.

Maybe there's an analogy with writing prose. I _could_ use rare words and uncommon syntax, but the modern style is a better approach. Is "abstruse" a rare word? Will a reader be frustrated to necessarily learn about split infinitives? What about I/O redirection at the beginning of a command?

Re: The useful use of cat

#103
post #10

Earlier quoted context omitted.

Yeah, there are still some edge cases where cat is useful, but more often than not I just begin the redirection. I reckon some don't realize you can put it at the beginning, which does sometimes feel more ergonomic in a pipeline.

It's not that it feels awkward to write the redirection first. It's that it feels awkward to write `< file cmd`, with no operator between the filename and the command, and the redirection operator not pointing at the command.

To me, the redirections never point toward or away from the command. Rather, they point into or out of the file. And really that's because they do: like, >file is really 1>file, and <file is really 0<file. I think if your intuition on how redirections work is based on whether it points at the command it's worth rethinking how you look at redirections.

Re: The useful use of cat

#104
post #92
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

> Using `cat file.txt | ...` has always felt more natural to me Use whatever you prefer¹, but if what you care for is the logic of reading left to right, you can do so without `cat` by doing instead ` ¹ It’s absurd this needs to be spelled out, but here we are.

No I can't. Because I use fish shell. `cat` can also be a single character if you alias it :)

Re: The useful use of cat

#105

You can still just begin with <somefile instead of cat somefile |. Does it really matter? No, but I don’t think pointing out useful and concise built-ins is necessarily asinine.

Something worth noting that I just noticed: on macOS (zsh), So I think I’d definitely tend to prefer <somefile when building up a pipeline. But certainly nobody should be shamed for doing things in the way that feels familiar and ergonomic to them!

Re: The useful use of cat

#107
post #92

Earlier quoted context omitted.

> Using `cat file.txt | ...` has always felt more natural to me Use whatever you prefer¹, but if what you care for is the logic of reading left to right, you can do so without `cat` by doing instead ` ¹ It’s absurd this needs to be spelled out, but here we are.

I don't care about fancy ' I want to type 'cat'. The visual imagery of summoning a cute cat to do my POSIX sorcery trumps all else.

Since when can you summon a real cat using the word cat? Or any word?

Re: The useful use of cat

#108
post #37

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…

Or you could just use cat

  cat file|grep x
  
The two alternatives are several keystrokes less and remove a useless process.

Re: The useful use of cat

#109

Earlier quoted context omitted.

My coworker said the same thing. But now that you know, will it give you pause? Shell has tricky and arcane corners that are best to avoid, but you still do better to know about them, lest they bite you (shellcheck helps). I don't think that "I/O redirection can precede the command name" is particularly tricky or arcane. What bothers me is that it doesn't work with loops: The shell's grammar is quite a thing.

“Subsetting” a language is a useful practice. Consider the set of language features someone has to know in order to understand your code. If you can make that set smaller (without unreasonably sacrificing functionality), you lower the barrier to entry. More people can read your code. The typical retort I hear is “but it’s just this one feature, how hard is it to learn just one feature?” But you’re only considering yo…

Readability is a concern, but it is only one concern among a bag of conflicting concerns, and it is meaningless to make decsions based on it before deciding the priority of all concerns.

Sometimes the readability is the most important feature and all other concerns can be compromised or sacrificed to readability.

But for me, usually not. As far as I'm concerned that is limited to training materials and showing your work in math exams. The rest of the time it's merely a goal after all other goals, ie you don't want things to be actually obfuscated, all else being equal.

Re: The useful use of cat

#110

Speaking of redirections... Here's another fut bit of lesser-known shell: f(){ cat;} I.e. You can bind redirections to function definitions that end up applying at call time.

what is a use of that vs f () { cat file; }

Why make the shell open and read the file and pipe in to the cat process when cat would open and read the file itself directly?

I guess the point is not cat but the redirection itself. ie, if the contents of f() were not simply cat, and maybe there was no handy place to put the filename within. Though, I think I still struggle to think up an example even then.

Post reply on HN