by jwhite · 9y ago · 0 comments
It took a lot of pushing from a mentor for me to finally click with the overarching principles of shell and see how to master it and become productive with it. He really forced me into it as well, I wasn't a willing pupil for quite some time, but he kept persisting and finally I started listening to him. I'm so glad I did. I've never read any book or tutorial or guide that teaches what he taught me either, despite searching several times. Also, I don't think I could have learned to master shell if I hadn't learned functional programming first. The day I realised how amenable shell is to higher-order thinking was a real epiphany.
These days shell is a really important tool in my toolkit, and I can see just how much some of my colleagues are held back by not knowing it.
Anyway, I really enjoyed the video, it was a nice exposition.
I got past all of that, and I also see people struggling to accomplish things that can be done in 30 seconds with shell. My main language is Python, and for certain tasks it just can't compete with shell. Believe it or not I think I once wrote a Python script to do what "find | sed -i" does.
Now I'm back in the "who would design such a silly language" mindset, and I am designing and implementing a new shell. You might like some of my blog posts:
"Pipelines Support Vectorized, Point-Free, and Imperative Style"
http://www.oilshell.org/blog/2017/01/15.html
"Shell Has a Forth-like Quality" http://www.oilshell.org/blog/2017/01/13.html (on HN previously)
Perhaps, but the shell is still a badly accrued (more than designed) set of options.
I wouldn't mind a better shell, but I'm not going to throw out my current shell for the lack of a better shell.
Something like ZSH and the modern Fish shell showed how much traditional shells can improve, but we also need some better primitives, and more controlled experience.
Oh, and they should NOT be based on emulating 40+ year old teletypes anymore, except as part of a "legacy" mode.
I guess maybe because I started with computers before there were GUIs that is what felt normal.
For me, probably not. I did use DOS before Windows as a kid, but that was long before really learning to use a shell. Familiarity with DOS simply made me familiar enough with the idea to be unafraid of it.
When I learned to manage files, handle software, etc. I learned to do it with a GUI in Windows. When I started to use Linux, I was vaguely aware of the usefulness of a shell, and took the opportunity to really learn. To this day, I rarely even have a file manager installed, in favor of a shell environment.
I also got my start on pre-GUI computers (a VIC-20, specifically).
https://www.gnu.org/software/bash/manual/html_node/Shell-Par...
That's the sort of thing that turned me off. I still tend to avoid those nooks and crannies, although they are sometimes useful. I'm not an expert.
I'm not sure about Unix shells being inspired by C semantics. I actually started out on csh or tcsh, I forget which, two decades ago. I'm not sure that making a shell language "C-like" is a useful goal, and those two shells both fell out of favour because they have many flaws. I switched to bash quite quickly.
- I used to spend a lot of time stuffing things into variables and then trying to operate on those, which is how you do things in imperative languages. Shell is better thought of in dataflow terms, i.e. pipelines. So I started using pipelines more and more.
- The corollary is that you start thinking more about data (passing between pipeline stages) and less about control flow.
- Compare an if statement in C, for example, to an if statement in a functional language. In C an if statement has no value per se, but in functional languages if "statements" are actually expressions that have a value:
f :: A -> String f x = if isFoo x then "foo" else "other"
In shell an if statement is just another command, so you can do things with its "value", i.e. its stdout:
if do_test
then
cat file1.txt
else
cat file2.txt
fi | grep pattern
Because I was so used to thinking of if statements as just control flow, I never used to think of piping them to another command like that.Also if you think carefully, that last pipe to "grep pattern" has the flavour of partial evaluation in functional programming.
- I tend to think more in terms of building up an execution environment these days, in the same way that lexical scope works in regular programming languages. In shell, of course, that means command invocations, sub-shells, etc., because the shell doesn't really have good lexical scoping. The simplest example I can think of is executing some commands in another working directory with a sub-shell:
(
cd $dir
run_test > "${f}.log"
)
You could compare it to RAII in C++ or with- style macros like WITH-OPEN-FILE in Common Lisp.Those are just some examples.
EDIT: Also, have a read of the links in chubot's post: https://news.ycombinator.com/item?id=14012453 . That's the sort of mentality that will change your approach to shell programming.