small pixel drawing of a pufferfish j3s.sh

add frontmatter to posix sh post lmao LETS GO
Jes Olson j3s@c3f.net
Fri, 10 Mar 2023 21:18:06 -0800
commit

51033b9095b14f98fd15767b8212b42b82536d48

parent

86f75a06516648907cb362123038a118a92dd87a

1 files changed, 150 insertions(+), 90 deletions(-)

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

@@ -1,6 +1,17 @@

-#!/bin/sh + write posix shell + 2023-03-10 + + /\ + {.-} + ;_.-'\ + { _.}_ + \.-' / `, + \ | / + \ | ,/ + [0] \|_/ + - what is shell? + what is shell? most people in tech are familiar with shell scripts. but shell is a language!

@@ -17,15 +28,6 @@

most people _hate_ shell. but should they? - - /\ - {.-} - ;_.-'\ - { _.}_ - \.-' / `, - \ | / - \ | ,/ - [0] \|_/ why do people hate shell?

@@ -60,56 +62,58 @@ simply: because shell is an insanely productive language.

in fact, i believe that shell is the *most* productive language. - 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 easily searchable knowledge. shell is also ubiquitous - and accessible almost anywhere you need it. + 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 easily + searchable knowledge. shell is also ubiquitous and accessible + almost anywhere you need it. - 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. + people often think that i work extremely quickly. + but here's the thing: i'm dumb as hell. - i just know a little bit of shell. + but i know a little bit of shell. - [2] .----. @ @ + [1] .----. @ @ / .-"-.`. \v/ | | '\ \ \_/ ) ,-\ `-.' /.' / -.................'---`----'----' +.................'---`----'----'______________*,___'_ bash me in the head - like human languages, shell has many dialects. there's the - common bash and zsh. let's see. there's also ksh. + like human languages, shell has many dialects. there's + the 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 + and 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. + a bash shell script only to later 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. - + to realize that it isn't compatible with linux. when people write shell, they will often say things like "i'm writing bash" or "i'm bash scripting" - they say this because bash is the most + + people say "bash" because bash is the most common shell dialect. - (bernie sanders voice) let me be clear: + (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 writing plain shell. + then you're just writing plain shell. the reason i'm being pedantic about "bash" is because i believe that most people should only learn and write

@@ -127,7 +131,7 @@ describing posix shell is simple. it is shell written

in accordance with the posix specification. the bad news is that the posix specification is documented - on the worst website i've ever seen[1], which i fully blame + on the worst website i've ever seen[2], which i fully blame for posix shell being unpopular. the good news is that if you write posix shell, there are

@@ -137,10 +141,11 @@ posix shell is compatible:

it'll run on debian, on openbsd, in an alpine container, - on macos! illumos! even fucking AIX! + on macos! illumos! + even horrible old AIX! posix shell is defined: - the posix spec fully defines how shell works. + the posix spec fully describes how shell should work. it defines every command, every flag, and every builtin function. it defines how loops work, how case statements ought to look, yadda yadda.

@@ -151,18 +156,20 @@ fun (aka: if you are a massochist), you might read

through the posix spec - it'd only take a day or two. or, you might just read the parts of it that - apply to the problems you solve, and piece it together - over time - that's what i did. + apply to the problems you're solving, and piece + it together as you go. 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" + 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" this feels good: - running posix shell commands that you reference directly from - the specification, knowing that they'll simply work - everywhere on everything for eternity. + running posix shell commands that you + reference directly from the specification, + knowing that they'll simply work everywhere + on everything for eternity. posix shell is eternal: imagine posix shell as the "standard library of shell"

@@ -172,16 +179,16 @@ same since. it is an important language that has lasted

30 years, and is very likely to last 30 more. - /------------------------------\ - | | - | omg! | - | posix shell is amazing! | - | i wanna use it everywhere! | - | i wanna go use it RIGHT NOW! | - \------------------------------/* - * - * - 🤔 <-- you + /------------------------------\ + | | + | omg! | + | posix shell is amazing! | + | i wanna use it everywhere! | + | i wanna go use it RIGHT NOW! | + \------------------------------/* + * + * + 🤔 <-- you slow down there you son of a gun!! yes, posix shell is amazing, but it's also very constraining.

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

- unlikely to increase in size and scope as time goes on - not very complex - if you breach the shell complexity ceiling, you should stop - immediately and use python or rust or whatever instead. + if you find yourself desiring arrays, good error handling, + static typing, structs, etc, then your problem should + be solved using a different language, not shell. - here are some simple ways to tell if your problem is too complex - for shell: + in this way, posix shell offers you a "sniff test" - 1. if you suspect that a shell program could be more - than ~200 lines at any point in the future + if something sucks to implement in posix shell, you + probably shouldn't implement it in shell at all. - 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. + when should i use shell? + in short; + when the problem you're solving is small, well defined, + and unlikely to change, consider shell. - when should i use shell? + when the problem you're solving involves linux, + text processing, or managing files, consider shell. - 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. + here are a bunch of examples of great little shell programs: - here are some great use-cases: + - mass rename files and make them consistent + (*.erb.html -> *.html) - posix shell inspo: + - make a take-my-estrogen alarm clock + (perhaps your laptop would beep) - dylan araps is the person who first inspired me to - dig into posix shell, and his projects[3] are - absolutely worth a browse. + - run a command when any .go files change - drew devault often promotes posix shell, and has used - posix shell to write a git client called shit. [4] + - find all files that end in .kfx and delete them forever - look at pa[5], a posix shell password manager that i - wrote. + - run commands on a bunch of remote systems at once - check out my dotfiles[6], which contains a ton of - little posix shell scripts that i use for all sorts - of things + - count the number of occurrences of the word "fuck" + across your projects, and sort the results + - make a shell program that backs your raspberry pi up + to an external hard drive - now get out there and write some fucking posix shell god damnit! + - submit JSON data to an endpoint on a schedule + + - check a git repo for updates and then run commands + (this is how i deploy j3s.sh!) + - attempt to predict rss feed endpoints and print any that exist + (my friend begriffs[3] and i wrote shell that does this) + + all of the above examples would take between 10 seconds and + 5 minutes to implement in the hands of an experienced + shell person. that person could be you!! + + + ✨ posix shell inspo ✨ + + dylan araps is the person who first inspired me to + dig into posix shell, and his projects[4] are + absolutely worth a browse. + + drew devault often promotes posix shell, and has used + posix shell to write a git client called shit[5] + + check out pa[6], a posix shell password manager that i + made. + + check out my dotfiles[7], which contain a ton of + little posix shell scripts that i use for all sorts + of things + + here's a cool arch wiki entry[8] on shells, check out + the posix section. + + + :D + + posix shell is fun, sloppy, powerful, portable, and eternal. + + wield it with caution, dear friend. but enjoy yourself. + + NOW GET OUT THERE AND WRITE SOME POSIX SHELL, GOD DAMNIT! + + ALL HAIL POSIX SHELL! ALL SNAIL POSIX SHELL! + + __,._ + / _ \ + | o \ \ oo + \___/ .|__|| + __,..="^ . , " , \ +............<._________________=/*___.__*___'___all snail!?____ + + NOW GET OUT OF HERE CUTIE! + + -jes + + + => refs [0]: https://archive.ph/KLpzo -[1]: https://archive.ph/higTn +[1]: ascii snail by Hayley Jane Wakenshaw +[2]: 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 +[3]: https://begriffs.com +[4]: https://github.com/dylanaraps +[5]: https://git.sr.ht/~sircmpwn/shit +[6]: https://github.com/biox/pa +[7]: https://git.j3s.sh/dotfiles/tree/main/bin +[8]: https://wiki.archlinux.org/title/command-line_shell