4 ms·
Amusing, though wish there were more/better examples. I not infrequently do something like this when experimentally/interactively building up a pipeline:
by downerending 6y ago
Amusing, though wish there were more/better examples.
I not infrequently do something like this when experimentally/interactively building up a pipeline:
cat somefile | foo | bar | baz
Even though I could say
foo < somefile | bar | baz
the former is clearer, and easier to edit if I'm shuffling things around a lot in real time.
And honestly, on modern hardware, worrying about an extra 'cat' is almost always a foolish optimization, unless you're doing it a zillion times.
I can probably make up other reasons why the 'cat' might be useful. For example, it makes it easier to separate the case of errors due to reading the file versus other errors, if that matters.
Also, 'cat' makes a nice "null" pipeline element if you need to slip one in for some reason.
And so on.
Perhaps one of the best "uses" of these "useless" commands is as a marker of the author's experience. When I'm working with someone who writes these sorts of things, I can adjust my expectations downward. :-)
- seniorsassycat 6y agoYour second form can be `<somefile foo | bar | baz` which preserves the order of your first example. And `<somefile` on it's own behaves as `cat somefile` (in my shell atleast).
- dllthomas 6y ago> And `<somefile` on it's own behaves as `cat somefile` (in my shell atleast). zsh? It doesn't work in sh, bash, csh, tcsh, or fish.
- JNRowe 6y agoOne of the zsh options which changes this behaviour is called CSH_NULLCMD, which seems to suggest it is possible to be csh compatible in zsh. $ setopt cshnullcmd $ < task >task2 zsh: redirection with no command `man zshmisc` and search for "REDIRECTIONS WITH NO COMMAND" for all the gory details :/ Edit: inverted csh compat logic in my head :(
- shakna 6y agoIn short, you seem to be right. Whilst fish will complain and tell you that it wants a command and not a redirection first, it should work in POSIX shells. sh -c '< tmp.c tail' Testing the above command it works: With Busybox 1.31.0. (Probably the least compliant.) GNU bash, version 5.0.16. However, dropping the command that is being passed to, doesn't result in output to stdout. Redirecting stdin to stdout doesn't seem to work either, but I'm not sure that redirecting stdin is all that well defined.
- dllthomas 6y agoRedirection is dup2, opening files first where relevant. If all you do is open files and dup around descriptors, no file contents actually go anywhere.
- donavanm 6y ago> Also, 'cat' makes a nice "null" pipeline element if you need to slip one in for some reason. It's a great way to get another 64KB kernel buffer in your command pipeline. Very useful for buffering your data stream when you have an impedance mismatch between the priority/scheduling/io/cpu time of the input and output.