small pixel drawing of a pufferfish j3s.sh

wip
Jes Olson j3s@c3f.net
Fri, 10 Mar 2023 20:03:30 -0800
commit

86f75a06516648907cb362123038a118a92dd87a

parent

b7dbb1b7fd1858c097c74cc6543281c76cee1b28

1 files changed, 120 insertions(+), 114 deletions(-)

jump to
M thought/write-posix-shell.htmlthought/write-posix-shell.html

@@ -2,20 +2,30 @@ #!/bin/sh

what is shell? - shell is a language. but most people in tech - are familiar with shell scripts. + most people in tech are familiar with shell scripts. + but shell is a language! ... shit. actually. wait, let me rephrase- - most people in tech _use_ shell scripts. - most people in tech _hate_ shell scripts. - most people in tech _avoid_ shell scripts. + most people _use_ shell. + most people _avoid_ shell. - and for good reason. + but mostly - because people hate writing shell (the language). + most people _hate_ shell. + + but should they? + + /\ + {.-} + ;_.-'\ + { _.}_ + \.-' / `, + \ | / + \ | ,/ + [0] \|_/ why do people hate shell?

@@ -26,8 +36,8 @@ because it was invented around the time

people thought lawn darts were a good idea. confusing error handling - in python there's try/except. in golang, errors are objects. - in shell, errors just sort of happen in insane ways. + in python there's try/except. in golang, errors are values. + in shell, errors sorta just happen in insane ways. hard to maintain when shell grows beyond a few hundred lines

@@ -40,7 +50,7 @@ which furthers confusion.

perception in the tech industry, people often look down on - shell scripts as tech debt / lame, and devs are + shell scripts as tech debt / lame, devs are often incentiviced to use hotter, more exciting things.

@@ -53,42 +63,57 @@

in terms of time + brainpower spent to produce a result, shell can do in 10 seconds what would take much longer in any other language, because shell is optimized for speed, instant feedback, and is enhanced by - many years of development. it's also ubiquitous, and accessible almost - anywhere you need it. + many years of easily searchable knowledge. shell is also ubiquitous + and accessible almost anywhere you need it. - people often think that they're not capable of producing things as - quickly as i can. but here's the thing: i'm dumb as hell. + people often think that they're not capable of doing certain work as + quickly as i can. but here's my secret: i'm dumb as hell. i just know a little bit of shell. -<ascii> + [2] .----. @ @ + / .-"-.`. \v/ + | | '\ \ \_/ ) + ,-\ `-.' /.' / +.................'---`----'----' bash me in the head like human languages, shell has many dialects. there's the - common bash and zsh shell dialects. there's fish. there's - ksh. dash. csh. mrsh. the list goes on. and on. and on. + common bash and zsh. let's see. there's also ksh. + oh, and dash. + and fish. + csh. + mrsh. + tcsh. pdksh. + the list of shell dialects goes on and on forever + like a slimy snail + + each shell dialect has varying levels of compatability + with the others. for example, imagine writing + a typical bash shell script using the only to + realize that it won't run inside of your alpine + container, because alpine uses dash. + imagine writing a shell script on macos, only + to realize that it won't run on linux. - each dialect has varying levels of compatability. you might use - a the bash dialect of shell, but struggle to run your scripts in - your alpine container. you might write a script on macos, - only to realize that it won't run on linux. when people write shell, they will often say things like "i'm writing bash" or "i'm bash scripting" - this is because bash is the most common shell dialect. + they say this because bash is the most + common shell dialect. - i want to be clear: - if you "write bash", you're really just - writing shell with some bash features. + (bernie sanders voice) let me be clear: + if you write shell and use some bash features, + you're writing shell in the bash dialect. if you happen to not use any bash features, - then you're just writing plain shell. + then you're writing plain shell. the reason i'm being pedantic about "bash" is because - i believe that most people should only care about a single - dialect of shell: posix shell + i believe that most people should only learn and write + a single dialect of shell: posix shell this opinion is not very widespread, but i think that it should be.

@@ -96,50 +121,55 @@

let me explain - ~eternal sunshine of the posix shell* + *eternal sunshine of the posix shell* - posix shell is simple. it is shell written by following - the posix specification. + describing posix shell is simple. it is shell written + in accordance with the posix specification. - the posix specification is hosted on the worst website - i've ever seen[1], and i partially blame that website + the bad news is that the posix specification is documented + on the worst website i've ever seen[1], which i fully blame for posix shell being unpopular. - if you write posix shell, you're in for some benefits. + the good news is that if you write posix shell, there are + huge benefits. posix shell is compatible: it'll run on debian, on openbsd, - an alpine container, - macos! illumos! even fucking AIX! + in an alpine container, + on macos! illumos! even fucking AIX! posix shell is defined: - the posix spec fully defines every shell command it allows - the defined commands have defined behavior - and have specific flags that do specific things + the posix spec fully defines how shell works. + it defines every command, every flag, and every builtin + function. it defines how loops work, how case statements + ought to look, yadda yadda. posix shell is pretty small: - if you like reading specs for fun (aka if you like torture), - you might read through the posix spec - it'd only take a day - or two. + if you enjoy reading technical specifications for + fun (aka: if you are a massochist), you might read + through the posix spec - it'd only take a day or two. - of, if you're not a massochist, just read the parts of it that + or, you might just read the parts of it that apply to the problems you solve, and piece it together - over time. + over time - that's what i did. - what feels bad: - running arcane shell commands from stackoverflow, having - them work, and being like "uhhhh wut.jpg" and moving on - because "shell is arcane magicks" + this feels bad: + running arcane shell commands copy&pasted from stackoverflow, + having them work, and being like "uhhhh wut" and moving on + because "shell is such a weird little guy" - what feels good: - running posix shell commands that you reference from + this feels good: + running posix shell commands that you reference directly from the specification, knowing that they'll simply work - everwhere for eternity. + everywhere on everything for eternity. posix shell is eternal: imagine posix shell as the "standard library of shell" - the concrete foundation on which you stand. + the concrete foundation on which you stand. posix shell + was first defined in 1992, and has remained largely the + same since. it is an important language that has lasted + 30 years, and is very likely to last 30 more. /------------------------------\

@@ -151,7 +181,7 @@ | i wanna go use it RIGHT NOW! |

\------------------------------/* * * - (you, probably) 🤔 + 🤔 <-- you slow down there you son of a gun!! yes, posix shell is amazing, but it's also very constraining.

@@ -171,81 +201,57 @@ - small and scoped (~200 lines of shell or less)

- unlikely to increase in size and scope as time goes on - not very complex - here's some simple rules to follow: + if you breach the shell complexity ceiling, you should stop + immediately and use python or rust or whatever instead. - 1. if you suspect that a shell script could be more - than ~200 lines, use a programming language instead. + here are some simple ways to tell if your problem is too complex + for shell: - 2. if your shell script needs arrays, structs, maps, or any - complex data structure, you have officially breached - the shell complexity ceiling and should stop right now. - use a real programming language instead. + 1. if you suspect that a shell program could be more + than ~200 lines at any point in the future - 3. if you need to do complex error handling or if the program - must be very reliable or + 2. if your shell program needs arrays, structs, maps, or any + other complex data structure + 3. if you need to do error handling, or if the program + must be very reliable - use a programming language. - pro posix shell tips from j3s + when should i use shell? - if you have a suspicion that the scope of your - shell script may need to grow over time, use a real - programming language instead. + it depends! i use shell every day, and i suspect that + many other people could benefit from using it every + day as well. i never regret learning more about posix + shell. - use #!/bin/sh at the top + here are some great use-cases: - do not use set -e unless you have good reason + posix shell inspo: - every command you use that is not in the posix spec you - should list as a dependency. + dylan araps is the person who first inspired me to + dig into posix shell, and his projects[3] are + absolutely worth a browse. - implement a die() function + drew devault often promotes posix shell, and has used + posix shell to write a git client called shit. [4] - keep your pipe chains short - - posix shell is all you need - - - be careful - - - - - posix shell - - posix shell is different. - - posix shell is the best idea with - the worst website i've ever seen.[1] - - the URL is horrific. the display is horrific. - but the content is golden. - - i often search for "posix shell <command>" and hunt - for the opengroup.org URL - it - - - my secret is shell. my "superpower" is shell. but not regular shell. - - can whip up a script in ten seconds that downloads a file, formats - its output, writes it to another file in the correct format. - - unfortunately, + look at pa[5], a posix shell password manager that i + wrote. - - maintenance: maintaining shell scripts is a nightmare - - + check out my dotfiles[6], which contains a ton of + little posix shell scripts that i use for all sorts + of things - bash + now get out there and write some fucking posix shell god damnit! - - -opinion: as a tech community, we need to write more posix shell. but not for the reasons you're imagining. - - - for inspiration, check out how russ cox uses shell to make decisions about - golang's development. - -[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html - sidenote: if anyone out there wants to write a posix shell website that doesn't suck absolute ass, let me know. +[0]: https://archive.ph/KLpzo +[1]: https://archive.ph/higTn + sidenote: if anyone out there wants to make a posix shell + website that doesn't suck absolute ass, let me know. +[2]: ascii snail by Hayley Jane Wakenshaw +[3]: https://github.com/dylanaraps +[4]: https://git.sr.ht/~sircmpwn/shit +[5]: https://github.com/biox/pa +[6]: https://git.j3s.sh/dotfiles/tree/main/bin