3 ms·
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 nec
by wwalexander 2y ago
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.
- jchw 2y agoYeah, 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.
- JoshTriplett 2y agoIt'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.
- jchw 2y agoTo me, the < and > 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.
- kortilla 2y agoA pipeline without pipes connecting is just a field of shit
- MathMonkeyMan 2y agoIO redirection doesn't construct a pipe, but piping does. # Open foo.txt and use it as fd 0 in jq <foo.txt jq '[1,2,3]' # Create a pipe(). Use the write end as cat's fd 1 and the read end as jq's fd 0 cat foo.txt | jq '[1,2,3]' Best thing would be a directed graph where each node is a process or a file. A process has at most one edge going into it (stdin) and at most two coming out (stdout and stderr). A file has exactly one edge touching it. Hard to do in text, though. Would look like lisp.
- AStonesThrow 2y agoNo, in ordinary usage it doesn't matter, and it's not like I would go out of my way to change someone else's script code. But if teaching or demonstrating shell features, I'd say that it's important to know that redirection is the most efficient, explicit, and available method. Indeed, a student could learn "cat file | ..." as a standard idiom and never use an initial redirect, but what happens when they come across one "in the wild"? They should know its proper interpretation, because it can otherwise be a bit jarring to see and difficult to interpret.
- kelnos 2y agoCan you? In bash I end up with no input at all if I start the line with '<somefile'.
- xelxebar 2y agoAhem, try this: <file cat
- wwalexander 2y agoSomething worth noting that I just noticed: on macOS (zsh), <somefile on its own seems to automatically page the output, whereas cat will simply print the entire file. 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!