3 ms·
Hahaha, lol. Where does this come from? I've certainly accelerated my Bash game since ChatGPT came to Earth. Do you know there's a syntax for defaults? ${MY
by keepamovin 3y ago
Hahaha, lol. Where does this come from?
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-brace
Reminds me of those codes on the phone system to get service functions...*69#
- blueflow 3y agoThat's called parameter expansion, see https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
- keepamovin 3y agoAlright, when you put it like that, it does make sense. Thanks, that's a great resource! I'm gonna start using ${MY_ESSENTIAL:?"Error: need to supply an essential"}
- rgrau 3y agoYou can also nest those, so this way you can get a value from an argument, or an env var, and if none is set, error. echo "${1:-${env_var:?I need a value for that!}}"
- keepamovin 3y agoOoh, I didn't know you could nest 'em! ChatGPT never told me that! Now we're getting somewhere! :) 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 v
- psd1 3y agoIt blows my mind how big a deal argument parsing is in legacy scripting languages (including python). That 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.
- blueflow 3y agoIf you call python a legacy scripting language, i'll call Windows (where powershell runs on) a legacy operating system.
- geocrasher 3y agoSure, Windows doesn't have "legacy" like VMS or Unix etc, but it's been in use by every day people longer than any other current OS except for MacOS, which really isn't the same OS it launched as, whereas Windows has maintained backwards compatibility (for better for for worse) for decades. So.. Legacy? Why not.
- keepamovin 3y agoThis is the most civilized flame war I have ever seen. :)
- psd1 3y agoI didn't mean to describe python as "legacy"; it is a great choice for scripting in the year 2023. It's second only to pwsh for that. 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.
- keepamovin 3y ago
- kyleyeats 3y agoI did use the file extension thing recently to differentiate between HTML and Markdown files. Very handy.
- sgarland 3y agoNot only is parameter expansion delightfully esoteric, it can replace sed for many applications. Or, as you’ve done, inverse basename. 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.
- Twey 3y agoHmm, I only see a little bit of bash in asdf, but it's mostly wrappers that I don't expect to have much impact on performance? E.G. https://gitlab.common-lisp.net/asdf/asdf/-/blob/master/make-asdf.sh https://gitlab.common-lisp.net/asdf/asdf/-/blob/master/make-... I always assumed asdf is mostly pure CL…
- twic 3y agoPresumably https://github.com/asdf-vm/asdf https://github.com/asdf-vm/asdf
- forgotpwd16 3y agoReplacing external program calls such as usual sed usage with shell (including bash specific) built-ins was also core in neofetch's design when was made as alternative to screenfetch. Will say that checking dylanaraps, someone worthy to be called a bash wizard, programs is a nice way to learn how to do complex shell scripting.
- yrro 3y agoI can't tell if this is a joke or not. 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.html https://www.gnu.org/software/bash/manual/html_node/index.htm...