93 points by LorenDB 4 days ago | 105 comments
Arcuru 3 days ago
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`
latexr 3 days ago
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 `< file.txt | ...`
¹ It’s absurd this needs to be spelled out, but here we are.
knighthack 3 days ago
I want to type 'cat'.
The visual imagery of summoning a cute cat to do my POSIX sorcery trumps all else.
latexr 3 days ago
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.
kazinator 2 days ago
oneeyedpigeon 3 days ago
latexr 3 days ago
I don’t think that’s what they’re saying. I think they’re making the point that going left to right with the file first is what makes sense to them, as opposed to `... < file.txt`.
oneeyedpigeon 3 days ago
latexr 3 days ago
And you’re assuming they do. It’s safe to assume at least one person reading the comment won’t know about the feature, the information is useful to more than that poster.
> but I can still see an argument for "cat" being more natural than "<" in this context.
You’re also assuming I don’t understand that sentiment. That has nothing to do with my original post. I don’t care what you use, do whatever makes you happiest. I was merely offering an alternative that would still satisfy what I perceived to be the original point (command piping left to right) to share knowledge, I was in no way criticising their method or saying they shouldn’t use it.
This is a useless conversation.
Arcuru 3 days ago
REDS1736 3 days ago
agumonkey 3 days ago
codesnik 3 days ago
<file grep something | do something else ...
this works in bash too, but if you're using zsh, there're a couple of nice shortcuts <file
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 << end
...
end
will give you nicer line editing experience.more zsh redirection tricks here: https://zsh.sourceforge.io/Doc/Release/Redirection.html#Mult...
globular-toast 3 days ago
<file grep ...
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.MathMonkeyMan 3 days ago
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:
<input.txt while read -r line; do thing; done # error
while read -r line; do thing; done <input.txt # fine
The shell's grammar is quite a thing.cellularmitosis 3 days ago
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 your favorite feature. To the casual code reader, it isn’t just that one feature, it’s everyone else’s favorite obscure feature as well.
Using cat makes your code readable to a wider audience. Using cat has real upside with no downside. Forcing your readers to learn more shell syntax has real downside with no practical upside.
MathMonkeyMan 3 days ago
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?
Brian_K_White 2 days ago
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.
latexr 3 days ago
Everything in programming is new to someone at some point. There are useful features few people know about, and you get them to be widespread and familiar by using and talking about them.
Just because you only ever learned about `for` loops doesn’t mean you shouldn’t understand and use `map`.
CBLT 3 days ago
g-b-r 3 days ago
Furthermore, a little memory lapse or typo, and, oops! >byebyefile
strunz 3 days ago
zahlman 3 days ago
HappMacDonald 3 days ago
`<fileA command` OTOH at least visually gives off the impression that it's sending the command's output to the left (changing the prompt, perhaps?)
oneeyedpigeon 3 days ago
chgs 3 days ago
mingus88 3 days ago
seba_dos1 2 days ago
kazinator 2 days ago
cat file|grep x
<file grep x
grep x file
The two alternatives are several keystrokes less and remove a useless process.gorgoiler 3 days ago
# bashisms
<file tr z-o m-g | …
FOO=bar python3 …
# processes
cat file | tr z-o m-g | …
env FOO=bar python3 …
shiomiru 3 days ago
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
So is FOO=bar cmd.
(I guess it is shell-specific if you include csh in the game, but then what isn't...)
MathMonkeyMan 3 days ago
I find myself googling "OpenGroup shell command language" pretty often to check this sort of thing.
kelnos 3 days ago
bloppe 3 days ago
sim7c00 3 days ago
Lockal 3 days ago
moffkalast 3 days ago