I have a confession to make: Unless shell script is absolutely required, I just use Python for all my automation needs.
Bash was the first language I learned, got pretty decent at it. Now what happens is I think of a tiny script I need to write, I start writing it in Bash, I have to do string manipulation, I say fuck this shit and rewrite in Python lol
Regex
Edit: to everyone who responded, I use regex infrequently enough that the knowledge never really crystalizes. By the time I need it for this one thing again, I haven’t touched it in like a year.
You get used to it, I don’t even see the code—I just see: group… pattern… read-ahead…
Don’t let the gatekeepers keep you out. This site helps.
No. Learn it properly once and you’re good. Also it’s super handy in vim.
interns gonna intern
You always forget regex syntax?
I’ve always found it simple to understand and remember. Even over many years and decades, I’ve never had issues reading or writing simple regex syntax (excluding the flags and shorthands) even after long regex breaks.
It’s not about the syntax itself, it’s about which syntax to use. There are different ones and remembering which one is for which language is tough.
This is exactly it. Regex is super simple. The difficulty is maintaining a mental mapping between language/util <-> regex engine <-> engine syntax & character class names. It gets worse when utils also conditionally enable extended syntaxes with flags or options.
The hardest part is remembering whether you need to use
\w
or[:alnum:]
.Way too few utils actually mention which syntax they use too. Most just say something accepts a “regular expression”, which is totally ambiguous.
Most of regex is pretty basic and easy to learn, it’s the look ahead and look behind that are the killers imo
PSA: Run ShellCheck on your shell scripts. It turns up a shocking number of programming errors. https://www.shellcheck.net/
Thank you for this. About a year ago I came across ShellCheck thanks to a comment just like this on Reddit. I also happened to be getting towards the end of a project which included hundreds of lines of shell scripts across dozens of files.
It turns out that despite my workplace having done quite a bit of shell scripting for previous projects, no one had heard about Shell Check. We had been using similar analysis tools for other languages but nothing for shell scripts. As you say, it turned up a huge number of errors, including some pretty spicy ones when we first started using it. It was genuinely surprising to see how many unique and terrible ways the scripts could have failed.
Maybe applies more to regex, the write only language.
The copy paste language. AI writes better regex than I do
and you won’t get better if you use ai for it
Meh I rarely use it. Even if I don’t use AI I wouldn’t get better at it, since I will forget everything the next time I will use it.
I still have to look up the exact syntax of ifs and whiles.
I’ve coded in bash for a while
Ever since I switched to Fish Shell, I’ve had no issues remembering anything. Ported my entire catalogue of custom scripts over to fish and everything became much cleaner. More legible, and less code to accomplish the same things. Easier argument parsing, control structures, everything. Much less error prone IMO.
Highly recommend it. It’s obviously not POSIX or anything, but I find that the cost of installing fish on every machine I own is lower than maintaining POSIX-compliant scripts.
Enjoy your scripting!
The sad thing is that even chatgpt can’t program in bash. I just want a simple script and every single time it just doesn’t work. I always just end up saying “write this in python instead”.
every control structure should end in the backwards spelling of how they started
Once you get used to it it is kind of fun.
Shame about
do
though.it could have been
not
since there’s notry
.
Je comprend tellement! Je répond en français pour ma première réponse sur Lemmy juste pour voir comment ça va être géré!
Si yo también comprendo, qué necesidad de comentar todo el tiempo en anglais?
En un mundo ideal. Todo se traduciría automáticamente del idioma original al idioma del lector y viceversa
¿No nos volvería lentos y flojonazos? (not a real word if you translate, more like slang meaning to be really lazy)
There’s always the old piece of wisdom from the Unix jungle: “If you write a complex shellscript, sooner or later you’ll wish you wrote it in a real programming language.”
I wrote a huge PowerShell script over the past few years. I was like “Ooh, guess this is a resume item if anyone asks me if I know PowerShell.” …around the beginning of the year I rewrote the bloody thing in Python and I have zero regrets. It’s no longer a Big Mush of Stuff That Does a Thing. It’s got object orientation now. Design patterns. Things in independent units. Shit like that.
I consider python a scripting language too.
They’re all programming languages, they all have their places.
All scripting languages are programming languages but not all programming languages are scripting languages
That’s why I use nushell. Very convenient for writing scripts that you can understand. Obviously, it cannot beat Python in terms of prototyping, but at least I don’t have to relearn it everytime.
Nu is great. Using it since many years. Clearly superior shell. Only problem is, that it constantly faces breaking changes and you therefore need to frequently update your modules.
They’ve slowed down with those a bit recently, haven’t they?
Yesterday, I upgraded from
0.101.0
to0.102.0
anddate to-table
was replaced equally (actually better) withinto record
, however it was not documented well in the error. Had to research for 5 to 10 minutes, which does not sound much, but if you get this like every second version, the amount of time adds up quickly.Not really. They’ve been on the stabilising path for about two years now, removing stuff like dataframes from the default feature set to be able to focus on stabilising the whole core language, but 1.0 isn’t out yet and the minor version just went three digits.
And it’s good that way. The POSIX CLI is a clusterfuck because it got standardised before it got stabilised.
dd
’s syntax is just the peak of the iceberg, there, you gotta take out the nail scissors and manicure the whole lawn before promising that things won’t change.Even in its current state it’s probably less work for many scripts, though. That is, updating things, especially if you version-lock (hello, nixos) will be less of a headache than writing
sh
could ever be. nushell is a really nice language, occasionally a bit verbose but never in the boilerplate for boilerplate’s sake way, but in the “In two weeks I’ll be glad it’s not perl” way. Things like command line parsing are ludicrously convenient (though please nushell people land collecting repeated arguments into lists).
So the alternative is:
- either an obtuse script that works everywhere, or
- a legible script that only works on your machine…
I am of the opinion that production software shouldn’t be written in shell languages. If it’s something which needs to be redistributed, I would write it in python or something
I tend to write anything for distribution in Rust or something that compiles to a standalone binary. Python does not an easily redistributable application make lol
For a bit of glue, a shell script is fine. A start script, some small utility gadget…
With python, you’re not even sure that the right version is installed unless you ship it with the script.
I try to write things to be cross-platform; with node builds, I avoid anything using shell scripting so that we can support Windows builds as well. As such, I usually write the deployment scripts in Node itself, but sometimes python if it’s supported by our particular CI/CD pipeline
I keep forgetting windows exists.
Most common development platform in the world
I quit using it in the WfW days and never looked back.
So true. Every time I have to look up how to write a bash for loop. Where does the semicolon go? Where is the newline? Is it terminated with
done
? Or withend
? The worst part with bash is that when you do it wrong, most of the time there is no error but something completely wrong happens.It all makes sense when you think about the way it will be parsed. I prefer to use newlines instead of semicolons to show the blocks more clearly.
for file in *.txt do cat "$file" done
The
do
anddone
serve as the loop block delimiters. Such as{
and}
in many other languages. The shell parser couldn’t know where stuff starts/ends.Edit: I agree that the
then
/fi
,do
/done
case
/esac
are very inconsistent.Also to fail early and raise errors on uninitialized variables, I recommend to add this to the beginning of your bash scripts:
set -euo pipefail
Or only this for regular sh scripts:
set -eu
-e
: Exit on error-u
: Error on access to undefined variable-o pipefail
: Abort pipeline early if any part of it fails.There is also
-x
that can be very useful for debugging as it shows a trace of every command and result as it is executed.
Knowing that there is still a bash script i wrote around 5 years ago still running the entirety of my high scool lab makes me sorry for the poor bastard that will need to fix those hieroglyphs as soon as some package breaks the script. I hate that i used bash, but it was the easiest option at the time on that desolate server.
Bash scripts survive because often times they are the easiest option on an abandoned server
I mastered and forgot almost entirely RegEx several times now