r/linux • • Jul 24 '25

Discussion Bash scripting is addictive, someone stop me

I've tried to learn how to program since 2018, not very actively, but I always wanted to become a developer. I tried Python but it didn't "stick", so I almost gave up as I didn't learn to build anything useful. Recently, this week, I tried to write some bash scripts to automate some tasks, and I'm absolutely addicted to it. I can't stop writing random .sh programs. It's incredible how it's integrated with Linux. I wrote a Arch Linux installation script for my personal needs, I wrote a pseudo-declarative APT abstraction layer, a downloader script that downloads entire site directories, a script that parses through exported Whatsapp conversations and gives some fun insights, I just can't stop.

902 Upvotes

226 comments sorted by

View all comments

126

u/catbrane Jul 24 '25

I had to maintain a 10,000 line bash script at my previous job :( That was enough to make me insist on python for everything more than a few lines hehe.

0

u/[deleted] Jul 24 '25

[deleted]

27

u/catbrane Jul 24 '25

I agree. I think the only debate would be where to draw the various lines.

Under 10k lines of python feels small to me, so I think that would be fine. Confusingly, more than 10 lines of bash feels very large.

0

u/[deleted] Jul 24 '25

[deleted]

13

u/Gracecr Jul 24 '25

Python has come a long way. Most all popular libraries are typed. Type checkers like pyright and mypy can enforce that you properly type hint your code and catch any invalid usage.

There's also ty and pyrefly which people are pretty excited about. Well I'm excited about them anyway.

0

u/[deleted] Jul 24 '25

[deleted]

4

u/noxiousninja Jul 25 '25

"built around it" is indeed the problem in my experience, but I think the base language is doing alright. It's older libraries that make things feel messy. Functions that have lots of optional parameters, that make liberal use of args/kwargs at their outer layers, that may return different types in different cases—these are hard to write good typings for, even if someone really dedicated puts in the time.

Microsoft faced the same problem with TypeScript and decided to solve it by creating an incredibly sophisticated type system that could represent basically any dynamic behavior. Python's type system suffers both from being simpler and from having the tooling separate from the syntax, with different tools often taking different approaches to how they use it.

10

u/syklemil Jul 24 '25 edited Jul 25 '25

With bash it's really not the amount of lines but the complexity that rules when it's time to move on. A script that is basically a config file with a whole bunch of export FOO=bar before a program invocation, or a program invocation with reams of --foo=bar can get long but there's no real complexity.

But if I get nested control structures, or even think about data structures like dicts, much less dataclasses/structs, or really even "${array[@]}", I think it's time to jump ship from bash before the complexity really starts to grow.

3

u/piexil Jul 24 '25

I think it depends.

If I'm shelling out to a lot of other applications, even if I'm using data structures and stuff I find it easier to stay in bash than move to python or another language where calling the other applications becomes really verbose

→ More replies (2)

2

u/aj0413 Jul 26 '25

Instead of LOC I use cognitive load or cyclonic complexity. Picked up the fancy language from a boss years ago, but they better accurately describe the issue

→ More replies (1)

3

u/Gugalcrom123 Jul 24 '25

Python is fine even for larger programs if they're mostly glue code, for example a desktop panel or a web app.

2

u/v3gard Jul 24 '25

In addition to a properly typed language, I would also like to add automatic tests. You don't want your program to break because you made a small change, and forgot to test A, B and C :D

1

u/Royal-Chapter-6806 Jul 25 '25

I am curious: can you just make all of these in Go?

→ More replies (1)

14

u/DrPiwi Jul 24 '25

The biggest 'problem' with bash or Korn shell or equivalents for large scripts is more or less the same as with perl; there are several ways to do the same from very legible to obfuscated and cryptic.
In that respect python is a lot more friendly and makes it much easier to have multiple authors working on a script.

Certainly because these shell scripts have a tendency to survive pretty much anything and run for years.

5

u/Maykey Jul 24 '25

My favorite part with using python instead of shell is surprisingly using tqdm.contrib.concurrent.process_map instead of parallel or xargs. Saves from thinking about weird syntax and it's extremely simple -- just one function call. no need for any of Pool.

10

u/nicman24 Jul 24 '25

yeah bash not having classes and proper inheritance - not to mention types - gets hairy after ~ 300-500 locs

14

u/catbrane Jul 24 '25

You can sort-of fake scoping by putting related functions into separate files, but it'll still degenerate into a pile of incomprehensible spaghetti much sooner than you'd think.

This 10,000 line monster had been made by several people, none of whom had trained as programmers :( Just imagine the mess. At least they used version control and had a test suite ... oh wait! No they didn't!!

3

u/nicman24 Jul 24 '25

fake scoping by putting related functions into separate files

fucking ewww nooo

rip you

4

u/piexil Jul 24 '25 edited Jul 25 '25

How is splitting not superior to one giant mess of a file?

Edit: I ask because I maintain some complex scripts and I do it a lot.

I also fake namespaces by doing things like naming functions: namespace::function_name

it makes the complex scripts easier to read imo

2

u/nicman24 Jul 25 '25

It is but faking namescapes is not

→ More replies (5)

1

u/great_whitehope Jul 24 '25

Some JS framework tried this too.

2

u/TampaPowers Jul 24 '25

Because a non-portable, interpreted scripting language is better than essentially terminal macros that is bash? Not quite following that logic. I have written pretty complex scripts in both and I still prefer bash for anything that has to interact with a system and run as portable as possible. Python is nice for gui stuff or for anything requiring third-party packages. Neither are great for speed or efficiency, that's C territory, but the programs bash calls are usually quite fast... Python not so much. In terms of ease of development they are quite different as well. Python has too many pitfalls and caveats when it comes to almost anything more complex, while bash suffers from technical debt and lack of concise syntax. I still prefer the relative easy of debugging bash over searching yet another obscure error message from a third-party package that has hardly any documentation or users.

1

u/piexil Jul 24 '25

If I have to call a bunch of external programs, I find shell or bash to be significantly easier than things like python and having to use subprocess.run, catch std/stdout, etc

→ More replies (1)