- cross-posted to:
- programmerhumor@lemmy.ml
- cross-posted to:
- programmerhumor@lemmy.ml
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 initialized 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.set -euo pipefail
Fun fact, if you’re forced to write against POSIX shell, you aren’t allowed to use these options, since they’re not a thing, which is (part of) the reason why for example Google doesn’t allow any shell language but bash, lol.
Btw, all three set options given above are included in POSIX since 2024: https://pubs.opengroup.org/onlinepubs/9799919799/
Ooh, you’re totally right!! I forgot about that since it’s not in the older versions.
I can only remember this because I initially didn’t learn about
xargs
— so any time I need to loop over something I tend to usefor var in $(cmd)
instead ofcmd | xargs
. It’s more verbose but somewhat more flexible IMHO.So I run loops a lot on the command line, not just in shell scripts.
This. But Pandas and Numpy.
Pandas and Numpy and Bash.
.loc and .iloc queries are a fun syntax adventure every time
Oh my!
And I thought I was the only one… for smaller bash scripts chatGPT/Deepseek does a good enough job at it. Though I still haven’t tried VScode’s copilot on bash scripts. I have only tried it wirh C code and it kiiiinda did an ass job at helping…
AI does decently enough on scripting languages if you spell it out enough for it lol, but IMO it tends to not do so well when it comes to compiled languages
I’ve tried Python with VScode Copilot (Claude) and it did pretty good
That’s because scripted languages are more forgiving in general.
I was chalking it up to some scripting languages just tending to be more popular (like python) and thus having more training data for them to draw from
But that’s a good point too lol
Both can be true, Python does have a lot of examples floating online.
Yeah I tried that, Claude with some C code. Unfortunately the Ai only took me from point A to point A. And it only took a few hours :D
I mastered and forgot almost entirely RegEx several times now
deleted by creator
Finite rules for perfectly sifting infinite options
deleted by creator
“mastered”
Mastered as in I could teach it to others, and assemble many complicated rules for many complicated patterns.
I am always impressed with folks that retain it.
I would a ton of it for a month or two, and then do nothing with it again for more than a year or more.
It takes a lot for permanent burn-in for me. That’s the curse. The blessing is that I learn very quickly.
I still have to look up the exact syntax of ifs and whiles.
I’ve coded in bash for a while
I don’t write very small shell scripts because I am not a job destroyer.
Me with powershell. I’ll write a pretty complex script, not write powershell for 3 months, come back and have to completely relearn it.
VLOOKUP. Every time. Also Catan Cities and Knights.
My google history hits for powershell for loop is is in the dozens.
Incredibly true for me these days. But don’t fret, shellcheck and tldp.org is all you need. And maybe that one stackoverflow answer about how to get the running script’s directory
I don’t normally say this, but the AI tools I’ve used to help me write bash were pretty much spot on.
IfWhen the script gets too complicated, AI could also convert it to Python.I tried it once at least, and it did a pretty good job, although I had to tell it to use some dedicated libraries instead of calling programs with subprocess.
For building a quick template that I can tweak to my needs, it works really well. I just don’t find it to be an intuitive scripting language.
Yeah, an LLM can quickly parrot some basic boilerplate that’s showed up in its training data a hundred times.
Yes, with respect to the grey bearded uncles and aunties; as someone who never “learned” bash, in 2025 I’m letting a LLM do the bashing for me.
Until the magic incantations you don’t bother to understand don’t actually do what you think they’re doing.
In fairness, this also happens to me when I write the bash script myself 😂
I wonder if there’s a chance of getting
rm -rf /*
or zip bombs. Those are definitely in the training data at least.The classic
rm -rf $ENV/home
where$ENV
can be empty or contain spaces is definitely going to hit someone one day
Yes, I have never wrote a piece of code that didn’t do what I thought it would before LLMs, no sir.
Sounds like a problem for future me. That guy hates me lol
Yeah fuck that guy
I have a confession to make: Unless shell script is absolutely required, I just use Python for all my automation needs.
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!
If you’re going to write scripts that requires installing software, might as well use something like python though? Most Linux distros ship also ship with python installed
A shell script can be much more agile, potent, and concise, depending on the use case.
E.g. if you want to make a facade (wrapper) around a program, that’s much cleaner in
$SHELL
. All you’re doing is checking which keyword/command the user wanted, and then executing the commands associated with what you want to achieve, like maybe displaying a notification and updating a global environment variable or something.Executing a bunch of commands and chaining their output together in python is surely much more cumbersome than just typing them out next to each other separated by a pipe character. It’s higher-level. 👍
If it’s just text in text out though, sure, mostly equivalent, but for me this is rarely the use case for a script.
I’m not anti bash or fish, I’ve written in both just this week, but if we’re talking about readability/syntax as this post is about, and you want an alternative to bash, I’d say python is a more natural alternative. Fish syntax is still fairly ugly compared to most programming languages in my opinion.
Different strokes for different folks I suppose.
Fish syntax is still fairly ugly compared to most programming languages in my opinion.
subprocess.run(["fd", "-t", "d", "some_query"])
vs
fd -t d some_query
Which is cleaner? Not to mention if you want to take the output from the command and pipe it into another one.
It’s not about folks with weird opinions or otherwise, it’s about use cases. 🙂 I don’t think python is any more “natural” than most other imperative languages.
Fish is probably even more natural, actually, due to it being more high level and the legibility of the script is basically dependent on the naming of the commands and options and variables used within it, rather than something else, just like python. They probably have similarly legible keywords. Fish I imagine has fewer, which is a good thing for legibility. A script does a lot more with a lot less, due to the commands themselves doing so much behind the scenes. There’s a lot more boilerplate to a “proper” programming language than a scripting language.
But if you want to do something that python is better suited for, like advanced data processing or number crunching, or writing a whole application, then I would say that would be the better choice. It’s not about preference for me when it comes to python vs fish, it’s about the right tool for the job. But if we’re talking about bash vs fish, then I’m picking fish purely by preference. 👍
I love fish but sadly it has no proper equivalent of
set -e
as far as I know.; or return;
in every line is not a solution.I’ve been meaning to check out
fish
. Thanks for the reminder!Happy adventuring! ✨
It’s the default on CachyOS and I’ve been enjoying it. I typically use zsh.
Yeah I also went bash -> zsh -> fish. Zsh was just too complicated to configure for my taste. Couldn’t do it, apart from copy pasting stuff I didn’t understand myself, and that just didn’t sit right.
I wish I could but since I use bash at work (often on embedded systems so no custom scripts or anything that isn’t source code) I just don’t want to go back and forth between the two.
Yeah, using one tool and then another one can be confusing at times. 😅
I switched to fish a while back, but haven’t learned how to script in it yet. Sounds like I should learn
Give it a shot after reading through the manual! (Extremely short compared to bash’s!) It’s a joy in my opinion. ☺️👌
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.
No. Learn it properly once and you’re good. Also it’s super handy in vim.
interns gonna intern
I just use the regex101 site. I don’t need anything too complicated ever. Has all the common syntax and shows matches as you type. Supports the different languages and globals.
This is one of the best uses for LLM’s imo. They do all my regex for me.
For me I spent one hour of ADHD hyper focusing to get the gist of regex. Python.org has good documentation. It’s been like 2 years so I’ve forgotten it too lol.
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.
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.
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.
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.
I give you that, true. I wish vim had PCRE
There is the “very magic” mode for vim regexes. It’s not the exact PCRE syntax, but it’s pretty close. You only need to add \v before the expression to use it. There is no permanent mode / option though. (I think you can remap the commands, like / to /\v)
twitch
You get used to it, I don’t even see the code—I just see: group… pattern… read-ahead…
Most of regex is pretty basic and easy to learn, it’s the look ahead and look behind that are the killers imo
(?=)
for positive lookahead and(?!)
for negative lookahead. Stick a<
in the middle for lookbehind.
Don’t let the gatekeepers keep you out. This site helps.
Nice! This is the one I use: https://regexr.com/
Though it appears to be very similar on the face of it.Chatgpt helps even more
I know that LLMs are probably very helpful for people who are just getting started, but you will never understand it if you can’t grasp the fundamentals. Don’t let “AI” make you lazy. If you do use LLMs make sure you understand the output it’s giving you enough to replicate it yourself.
This may not be applicable to you specifically, but I think this is nice info to have here for others.
I have no interest in learning regex ever in my life, I have better things to dedicate my brain capacity to haha
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
.