Being a bash developer in the 21st century (2021)
blog.barysas.eu
blog.barysas.eu
We bash developers are in a really bad spot. If you know one of us, please check on him or her. ChatGPT has enabled our digital hoarding like never before (some of us call it a "scripts folder"-- check for files like build5.sh or *.yml for really bad cases).
I don't know how to write bash. But I'm doing for loops and if statements like never before. I justify it by saying I'm only hurting myself. I've already gone through one hard drive, writing a new index.html over and over and over to the same spot, burning it into the device like the "2" in Sonic the Hedgehog 2 on a 90s CRT.
I lost one of my best friends after I asked him why we don't just use string literals and yaml files for everything. Another when I told him to just put his CSS in a style tag. I can't stop. I can't go back. All I can do is stay away from Makefile people and hope it doesn't get even worse. Don't write shell scripts, kids.
I've certainly accelerated my Bash game since ChatGPT came to Earth. Do you know there's a syntax for defaults?
${MY_BIG_VAR:-your_tiny_default}
Or that you can completely non-sensically extract the file extension with? file_extension="${1##*.}"
How to pronounce that? dollar open-brace 1 pound pound star dot close-braceReminds me of those codes on the phone system to get service functions...*69#
${MY_ESSENTIAL:?"Error: need to supply an essential"} echo "${1:-${env_var:?I need a value for that!}}" echo "${1:-${env_var:?I need a value for that!}}"
That is awesome! :)Like massively concise construct. I don't know what other language can compete:
console.log(process.argv[2] || process.env.VAR || console.error("I need a value for that"));
I personally prefer the bash vThat snippet is concise, but relies on the user supplying arguments in the order you demand. AFAICS, non-trivial scripts require you to loop over arguments, testing against long and short forms of parameters, and shifting if the parameter expects an argument.
In Powershell, you define your parameter and apply constraints. Voila:
[Parameter(Mandatory)]
[ValidateRange(1, 20)]
[int]$foo
That's it, the entire thing, done.I use pwsh on Linux every day.
Anything that isn't a lisp machine is a legacy OS ;-)
I realise I've given offense, for which I'm sorry, but I'm driven not so much to denigrate bash as to advocate for a world where shells are semantic.
Syntactically I'd even prefer a single line, like
[Pram(musthave):In(1, 20)/"Foos excessive or inadequate":int/$foo:"Foo count"/"Please supply how many foos you want."]Since it’s a shell built-in, it avoids the overhead of exec. Ever wonder why asdf is slow? Go peek at the source code. This is why. I have a draft PR that fixes it, but there are still some failing tests I need to fix.
I always assumed asdf is mostly pure CL…
Don't take this the wrong way but doesn't anyone ever read the manual any more?
https://www.gnu.org/software/bash/manual/html_node/index.htm...
With python or rust or whatever a compiler/toolchain basically only lasts 3 months because some new feature is added and used that makes your rustc compiler from 4 months ago unable to compile it. Or with python you pretty much have to assume you system python won't work and install an entirely new toolchain using a dependency manager manager to manage your containerization and then within that (venv, pyenv, conda, etc, etc) a dependency manager (pip, PyPI, etc) to actually get the lib versions you need. Only then can you start trying to actually execute the program.
Bash? Just chmod +x and run the script. It'll work no matter where you're running it. And it won't stop working.
Shells are good at starting, stopping, backgrounding processes and piping, redirecting and substituting output that they make programming languages feel very clunky imo. Plus you can now almost expect bash to always be installed on a machine somewhere.
It lists something like 15-20 technologies to build the site. I guess most of them are beneficial, but... why are some so proud of how large their dependency tree is?
[0]: https://kentcdodds.com/blog/how-i-built-a-modern-website-in-...
Look, I'm pretty familiar with other languages, but the fundamental deal is this:
I don't write programs for anyone but myself. And I've seen no reason to switch to anything else, even python.
I get that things get ugly, but I absolutely prefer my own ugly to someone elses boring? I want to make things happen now.
I bet vscode can do the same.
But I would try VS Code just to go through my shell files with shellcheck! :)
here the link to the config: https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main...
both work really well
Today I needed to parse some JSON at the command line and do some basic search and replaces and convert it all to CSV. The API call was easy enough with wget, and I was going to use JQ to parse the JSON. But after fiddling with it for 20 minutes and not having much luck, I resorted to my favorites:
Awk, sed, grep, cut. Save API call output to a couple of temp files, parse it with the aforementioned Unix staples in a for loop, And unless time than it would have taken me to properly learn JQ, I had the output and was able to ship it off to my customer.
Sometimes the best tool for the job is the tool you know, love, and trust.
Saved my sanity more times than I can count, and allows me to avoid the Cthonic black magic known as `jq`.
That being said, I'll check it out on my local machine :)
> Today I needed to parse some JSON
You had some JSON blob, and you actually needed to glean something from it quickly and painlessly. To paraphrase Zawinski et al., you then found you had two problems.
In this age of big data, the debate between parsable and human-readable state has been resolved. JSON is an anti-pattern that requires supplementary tooling to make any sense of it, nevermind naive readability.
https://sipb.mit.edu/doc/safe-shell/
I say this as a diehard shell fanatic. The closer to metal, the better. ;-)
I’m good with -u on its own, but the others, meh… that’s what shellcheck is for.
EDIT: Read this [0] and weep/revel in its glory, depending on your POV.
Hard disagree about -e though. I get it's not perfect but it has probably saved me dozens of hours of frustration and heartbreak. If you forget to handle errors for a particular line, then the script will just keep going and that's how you accidentally delete your entire home directory. Sure you can rely on shellcheck but for me personally I don't have shellcheck available 100% of the time as I often find myself writing scripts as raw strings (e.g. ssh -c).
I prefer having a stop-on-unexpected script as it makes errors far more explicit and it's not too onerous to work round the peculiarities of return codes.
Greg's wiki (https://mywiki.wooledge.org/) is my go to resource for looking up snippets and learning to avoid the footguns - that and shellcheck are the key to "robust" bash scripts.
I would be happy to find more bash script(er)s out there, to find inspiration and solutions for problems that increase my horizon of what's possible.
Kudos to OP!
https://mywiki.wooledge.org/BashFAQ
Also, always validate your scripts using shellcheck (https://www.shellcheck.net/)