by woodruffw · 4y ago · 0 comments
I learned how to use a POSIX shell years before I learned how to program (outside of a shell, that is), and I've often wondered how to help people (particularly younger colleagues) who learned things the other way around.
My experience so far is that a lot of people get stuck in an "experience trough," for lack of a better phrase: they're comfortable navigating around the filesystem and running basic commands, but struggle with some of the more powerful building blocks that can make someone a real shell power user (`xargs`, nontrivial uses of `find`, here-strings, subshells and blocks, process substitution, etc.).
That "experience trough" you describe I think exists because they don't actually understand the fundamentals, so any time they've attempted something new it's based on assumptions from a totally different paradigm, does something strange and unexpected, and reinforces sticking to the same safe and simple commands.
It's just a different mindset. Instead of a std library you have tons of prebuilt binaries ready to perform actions and talk to one another. And that is really, really, powerful once you learn what's all available and how to use/abuse it.
Software engineering in general is so different. Tons of mantras to follow, TDD, OOP, etc.
Each has its place, but generally if I can do it in a few lines of shell, why spend 20 minutes writing a hundred lines of something more pure? On the other hand, shells do not seem well suited for anything large and complex, admittedly. So watching people waste an hour writing something that could be a Bash one liner is about as painful as watching someone try to debug a 500 line bash script.
I’m not really sure it’s fair to assume that you can use advanced topics after reading any book aimed at beginners.
Also, no one is going to learn how to use any of your advanced topics unless they have a need. And people rarely have a need for those things.
For me, it was a lot of scripting and then iterating on those scripts when I realized that they conceptually broke down in places (even if they never broke in practice). Things like spaces where you don't want them, needing to pass around data that might be too large for `argv` (because of stack limits), etc.
This is very useful for those who are stumped by a failing script.
"I am a strong believer that Bourne-derived languages are extremely bad, on the same order of badness as Perl, for programming, and consider programming sh for any purpose other than as a super-portable, lowest-common-denominator platform for build or bootstrap scripts and the like, as an extremely misguided endeavor. As such you won’t see me spending many words on extensions particular to ksh, Bash, or whatever other shells may be popular."
For me, `cat | grep/sort` is easier to remember as it follows some unixy philosophy on how to do things.
I wonder why that article author is so obsessed with putting down cat/pipe. Maybe in scripts where you process large files and sort/grep it is less ineffective. But when writing shell commands, I see this as an empty argument to retrain my "muscle memory" and learn "different syntax".
In fact, when writing powershell I do the same: `gc something | sls something`. But I know it is less effective, but I'm lazy to look up proper syntax: `sls ?? something` - But if I ever will need that in a script to process non trivial amount of files-lines - I'll look it up.
I am one of those... as soon as I don't know how to do something in the shell, though, I just fire up a REPL or a "scratch" in the IDE and write what I want in a "proper" language. Sorry, but shel scripting is not always the best way to go. I eventually did learn me some AWK and that was useful, but most of the time I don't use even that, it's just so much easier for a programmer to use a programming language to do stuff.
for item in some_list:
foo(item)
Of course, the above is a replacement for "xargs -n 1" only; but I argue that the batching functionality of xargs isn't really needed in programming languages.