this post was submitted on 06 Aug 2023
609 points (97.4% liked)
Programmer Humor
19729 readers
102 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I can't think of a single reason to use bash over Python. Anything you can do in bash can be done in pure Python. Unless you're working in some embedded environment it's a non-issue to install a Python interpreter (you certainly already have one). I would only use sh/bash for packages I'm distributing to avoid the external dependency, and then only if it's a relatively simple script.
I know whatever environment I run my shell script in has
sh
, I can't rely on (the right version of) python being there.Why not?
Because you could be on another machine that doesn't have Python 3.X it only has 3.X-1. or you could write code for Python 3.12 and then four years later no one has 3.12 anymore.
Sometimes you need to download packages from pip but pip might not be available or you may be hitting your company's internal pip mirror.
How often are you writing scripts that accidentally require a specific minor version of Python 3 to run? If you have dependencies, 1) you're no longer scripting, and 2) you need to manage your runtime environment anyway.
Runtime environments can change often, if you need to install your application on a ton of PC's you don't want to install python version 3.X on all of those instead you could just compile it into a native binary, pip is the most unsafe hell
If you're compiling a native binary you DEFINITELY aren't in scripting territory anymore.
Bash is much better for doing file operations and piping the output across multiple commands
Better than subprocess.getoutput?
i would not run a python script with root.
That's fair.
i run my daily NAS backup python script with root.
It is fine, just should be more careful with modules used.
Why is always about bash? POSIX shell scripts run everywhere.
BASH has some useful features and I’ve literally never had an environment with bash unavailable (even if a package is needed, so what).
MacOS ships only 3.something version, which has some compatibility issues with Bash 4+.
brew install bash
Granted, it's a kind of niche use case but in the embedded world there's usually size constraints which prevents a full blown bash installation.
However, things are better now than 20 years ago. Flash is cheap.
Python is superior for string anything (parsing, searching, manipulating). But Bash is much simpler for running existing CLI tools. Plus you should already be using Bash as a simple terminal language already, so wrapping what you're used to into a simple script flows naturally.
Eg, if I have some admin tool for updating a user thingamajig, a common scripting need is just running that tool for every user in a file (or the output of another command). The string manipulation that often requires is annoying in bash, but running the commands is easier than Python.
If what you're doing is essentially a few shell commands, then you may as well put it into a script. If you're talking about how "elegant" your shell scripts are and comparing them to Python, you're probably wrong and should be using Python.