5 ms·
If looking at the common pipe chains from shell histories tells me anything, it is that people are not very familiar with the tools at their disposal. Just loo
by brbsix 11y ago
If looking at the common pipe chains from shell histories tells me anything, it is that people are not very familiar with the tools at their disposal.
Just look at some of these chains:
ps | grep
cat | grep
find | grep
find | xargs
grep | wc
ls | grep
echo | grep
grep | grep
The legitimate uses for those pipe chains (while they do exist) are few and far between...
A particularly odd one on the list was `type | head`. Does anyone know the purpose of this?
- Perseids 11y agoThe legitimate use comes from real world situation. Quite often I first cat a file only to find out it is too large to look through by hand and only then grep it. As the last command was `cat /some/long/path`, I go up to the last command and just add the grep to the end (and thus end up with cat | grep). Likewise, I vaguely remember that grep has a count switch, but looking it up in the manpage is more work than using wc (-> grep | wc). And likewise, before I would use find's -exec command I would need to look up the precise syntax again, because there where some details regarding character escapes, IIRC. Remember, we are always intermediates at most things[1]. [1] https://blog.codinghorror.com/defending-perpetual-intermediacy/ https://blog.codinghorror.com/defending-perpetual-intermedia...
- steveklabnik 11y agoI'm familiar with these tools, but still `| grep`. Once you've learned the "compose little tools" philosophy, it's hard to get it out of your head.
- electroly 11y agoThis isn't "shell golf". This idea that we shouldn't use small focused tools in a chain, but rather we should find as many arcane arguments and switches as we can to shorten the chain, is contraindicated by the Unix philosophy. I don't know why this nitpick comes up so often. There are many "human factor" reasons why a longer chain with simpler commands is desirable.
- brbsix 11y agoIs it really so arcane to `grep PATTERN FILE` or `grep PATTERN` <kbd>Alt .</kbd> (if the previous command was `cat FILE`)? Is it also arcane to `pgrep PATTERN` instead of `ps aux | grep PATTERN`? Is it also arcane to `egrep 'PATTERN|PATTERN'` instead of `grep PATTERN | grep PATTERN`? Personally I prefer the "correctness" of this sort of approach, but the tools are just a means to an end and understandably people have varying preferences. Ironically "legitimate" was probably not an accurate choice of words.
- zwp 11y ago> `egrep 'PATTERN|PATTERN'` instead of `grep PATTERN | grep PATTERN` Oops? Ironically (assuming two distinct values of PATTERN) I think you just answered your own question. (They are different: first is disjunction of patterns, second is conjunction). Your point has merit for scripts (performance) but for data exploration at the prompt it's almost always irrelevant: the simplicity of pipe composition outweighs anything else.
- brbsix 11y agoWhoops you've got me there. Yes for that example, the grep alternative is not very elegant. Anyways I wasn't making an argument against composition, just particular types of composition (such as useless use of cat, parsing ls, grepping ps) for which there are side-effects or there is a simpler or more appropriate alternative.