The Shell Hater's Handbook (2010)
128 points by samlambert 8 months ago | 53 comments- packetlost 8 months agoYouTube link for video if it's broken for others and not just me:
https://www.youtube.com/watch?v=olH-9b3VJfs
Something I learned recently is that the Bourne shell (and by extension, bash and POSIX's sh) have syntax inspired by Algol 68 (source [0]), which explains some of the funkyness. One thing I've been doing recently is writing scripts in rc, the default shell for plan9. It's a bit saner syntax-wise IMO. Versions linked against readline have file-based completion, but it's otherwise not quite robust enough for me to switch away from fish as my default, but it has some things I prefer over both bash and fish.
I encourage people to give rc and awk a shot, they're both small and learnable in an afternoon!
- Rendello 8 months agoThe Bourne shell was hilariously written in horribly deformed C resembling ALGOL by way of macros:
https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh...
- packetlost 8 months agoThat's amazing
- packetlost 8 months ago
- Joker_vD 8 months agoInfluence of Algol 68 doesn't even really explains requirement of semicolons (or equivalently, new lines) in weird places.
argubaly should just work, the presence of do/then/done/fi keywords makes semicolons quite superfluous yet the correct forms arewhile false do echo 1 done if false then echo 1 fi
Which is strange, because Algol's grammar actually prohibits ; before ELSE, FI, and OD keywords yet the Bourne shell requires them!while false ; do echo 1 ; done if false ; then echo 1 ; fi
- Koshkin 8 months agoWell, to be fair, the use of semicolons in the shell has nothing to do with their use in other languages, here they are simply line separators.
- Koshkin 8 months ago
- jorvi 8 months ago> One thing I've been doing recently is writing scripts in rc, the default shell for plan9. It's a bit saner syntax-wise IMO
I'll stand on the point that if you're gonna forego (ba)sh compatibility, 95/100 times you might as well write Python scripts. Shell syntax generally sucks, and the only reason we still roll with it is legacy code, universal compatibility, and pipes.
- packetlost 8 months agoAs someone who worked with Python professionally for like 8 years, I wholeheartedly disagree with this. Python is an ok scripting language but it's an awful command script language. The extra ceremony to just invoke another command is a complete non-starter IMO. Ruby handles the cases where a shell script makes sense way better than Python does and I still wouldn't pick Ruby. Rc's syntax is better than ba(sh), which is the point: you get things like pipes and easy commands without some of the cruft.
- setopt 8 months agoAs someone who also has written my fair share of Python, I completely agree. Piping and subprocess handling are my two main complaints as well.
Managing concurrent processes is arguably also easier in shell scripts, in that you can just append “&” to run stuff in the background and “wait” to sync.
- setopt 8 months ago
- packetlost 8 months ago
- Rendello 8 months ago
- hamandcheese 8 months agoI've grown rather fond of bash in my current role. I work mainly on developer tools and CI pipelines, both of which mean gluing together lots of different CLI tools. When it comes to this kind of work I think it is quite hard to beat the expressiveness of shell scripting. I say this as a former hater of bash and its syntax.
Much credit to copilot and shellcheck, which have made complex bash ever the more write-only language than it already was.
- eadmund 8 months ago> I've grown rather fond of bash in my current role. I work mainly on developer tools and CI pipelines, both of which mean gluing together lots of different CLI tools. When it comes to this kind of work I think it is quite hard to beat the expressiveness of shell scripting.
Every time I have to express logic in YAML, I miss shell. Shell’s really not great, and it could be improved upon (my vote? Tcl), but it’s so much better than where the industry is these days.
- hollerith 8 months agoYou can't use a shell script or a TCL script to generate the YAML file?
- eadmund 8 months agoSure, you can generate the YAML file, but it’s not generally possible to trigger that from the system which wants the YAML file, and you don’t get to integrate with the system’s configuration. Often these systems honestly think that their approach of logic-templated YAML is preferable to a script — for example Helm’s templated Kubernetes YAML.
- eadmund 8 months ago
- hollerith 8 months ago
- chasil 8 months agoBash actually has more warts than competing shells because of its historic stance.
My bugbear is that "alias p=printf" works well in any POSIX shell script, including bash when it is invoked as #!/bin/sh - but when called as #!/bin/bash, the alias (used in a script) fails with an error.
While the Korn shell managed to evolve and comply with the POSIX standard, bash decided to split the personality, so one solution to the above alias failure is to toggle POSIX mode.
Bash was forced to do this, to keep a decade of shell scrips that were written for it working. Pity.
The standard for the POSIX shell looked very hard at Korn, and credits it. Bash is not mentioned.
- mort96 8 months agoHuh, why wouldn't that alias work?
- 8 months ago
- photon-torpedo 8 months agoGood question. Something to do with interactive mode?
Edit: Ah yes, the man page says so.$ cat l.sh alias l=ls l $ sh l.sh file1 file2 l.sh $ bash l.sh l.sh: line 2: l: command not found $ bash -i l.sh file1 file2 l.sh
> Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt
- 8 months ago
- cduzz 8 months agoI typically just give up on weird corners of shell when I find an working version of the same.
For instance -- why would you use "alias" when you can make a function? The syntax is a little weird with functions, but it's a lot more clear what's going on.
The same goes for "test" vs the seeming magic of [ where it seems like [ is language syntax (it's a single character!) when in fact ... it's just another executable that communicates with logic evaluation like anything else (like grep or false).
- alganet 8 months agoThere are many reasons.
1. Aliases can work with the callee scope.
Calling the alias by the name will set the callee's variable foo, while calling the function does nothing (local foo is local to the function and never leaves scope).alias MY_VAR_MODIFIER='local foo=bar' MY_VAR_MODIFIER () { local foo=bar; }
It also works similarly for working with the set ($@). You can do `set --` stuff and it works on the scope of the callee.]
Aliases on most shells also don't need to be fully valid _before_ expansion. You can alias a compound command:
Only ksh93 will complain about it, it requires aliases to be complete valid shell programs.alias foreach='for EACH' foreach in $MYVAR #perfectly valid for most shells.
Finally, alias calls don't appear in xtrace (set -x). Only the final expansion will appear.
TL;DR aliases in scripts work a lot like macros.
- alganet 8 months ago
- mort96 8 months ago
- IshKebab 8 months agoGluing together tools with shell scripts is a significant cause of CI failures in my experience. There's no reason to do it. Use a real language - at least Python, but my preference is Deno because it's not dog slow and you don't have to deal with venv.
- Spivak 8 months agoI mean I guess but what's your plan when your script is just a bunch of subprocess.run calls? I promise you it won't be any less brittle.
- tom_ 8 months agoThe subprocess.run args argument is a list of strings, not a single string with whitespace-delimited parts, so you're all good quotingwise. This is now effortlessly a lot better than what you get with bash in terms of how brittle things are!
Supply check=True and the script will barf on subprocess failure. Another useful upgrade.
- iamjackg 8 months agoTake a look at https://sh.readthedocs.io/en/latest/ for a very usable solution to easily run other processes in Python. It has made my life a lot easier whenever I've had to migrate a shell script to Python.
- IshKebab 8 months agoI promise it will! It will handle weird filenames properly for example.
But realistically it's rarely that simple and even when it starts that simple it will grow to not be.
The downvotes are a very disappointing attitude.
- tom_ 8 months ago
- Spivak 8 months ago
- eadmund 8 months ago
- genezeta 8 months ago(2010)
Sadly the video seems to be missing; the whole GoGaRuCo conference site is gone, actually.
Maybe it makes more sense to post the link to YT, as you did in 2022 https://news.ycombinator.com/item?id=32521080
- sundarurfriend 8 months ago> Maybe it makes more sense to post the link to YT, as you did in 2022
Based on the comments there, it seems like OP posted this current (shellhaters.org) link then too, and it's the mods that replaced it to point to the YouTube video. Hopefully the same will happen this time around as well.
- sundarurfriend 8 months agoI tried following the slide deck (to get an idea of the content), but that's pretty rough too. The page reloads itself constantly and is very inconsistent with responding to input keys, it's not a pleasant experience.
Thanks for the (indirect) link to the YouTube video, will give that a try.
- sundarurfriend 8 months ago
- yanowitz 8 months agoAnd its inspiration is still great but could use a refresh: https://web.mit.edu/~simsong/www/ugh.pdf
- musicale 8 months agoA brilliant historical artifact, and the irony that macOS has been Unix for most of the Mac's existence, and that Windows has included POSIX and/or Linux for most of its existence, is not lost on me.
The Dennis Ritchie anti-forward makes it even better:
> The rational prisoner exploits the weak places, creates order from chaos: instead, collectives like the FSF vindicate their jailers by building cells almost compatible with the existing ones, albeit with more features. The journalist with three undergraduate degrees from MIT, the researcher at Microsoft, and the senior scientist at Apple might volunteer a few words about the regulations of the prisons to which they have been transferred.
- alkh 8 months agoThanks for reminding me about this gem! :)
- musicale 8 months ago
- Eisenstein 8 months agoGetting a DNS error from confreaks.
You can get the video here:
* https://web.archive.org/web/20140207100122/http://confreaks....
- lproven 8 months agoA video is not a handbook. A handbook is a book. There's a clue in the name.
Life is too short for videos.
- nlawalker 8 months agoI'm not familiar with the landscape - is there a "compiles to reasonably-readable [ba]sh" language that's gotten any traction anywhere? That seems like at least an interesting possible solution.
- chamomeal 8 months agoThere was a pretty great-looking one that I saw on HN in the last few months. I didn’t end up trying it, and I don’t remember what it was called.
There’s also babashka, which pretty much let’s you write clojure in your shell scripts.
- dmckeon 8 months agoMight have been Oil shell, which is worth looking at: https://www.oilshell.org/
- simonmic 8 months agoOil shell is the way forward for shell.
- simonmic 8 months ago
- bewuethr 8 months agoWas it https://amber-lang.com?
- dmckeon 8 months ago
- chamomeal 8 months ago
- phendrenad2 8 months agoIf you're a shell hater, the answer isn't to write MORE shell, but to switch to something else, for the love of all things sane and normal.
- lproven 8 months agoThe trouble is that all the existing xNix GUI shells suck. They suck in widely varying degrees, from ones that could suck a bowling ball through an iron water pipe, like GNOME and KDE, to ones that could merely suck a golfball through a hosepipe, like Unity and Xfce, but they all suck.
There have been GUI shells that didn't perceptibly suck at all, such as early versions of the Windows 9x shell, classic MacOS, and Acorn RISC OS, but they're all largely dead and gone now.
- phendrenad2 7 months agoWell, I meant people should use a real programming language. Also, I'm not sure why "bash with a real programming language syntax" isn't a thing. Apparently last time this was tried was the "C Shell" which had c-like syntax? Why don't we have a javascript-like syntax for a shell yet?
- lproven 7 months agoEr. I think we're at cross purposes.
What do you mean by the word "shell"?
I mean "the user interface of the OS" which implies nothing about whether it's text, graphics, both, spoken word, gestural, whatever.
- lproven 7 months ago
- phendrenad2 7 months ago
- lproven 8 months ago
- 8 months ago
- dxdxdt 8 months ago[flagged]