12 comments

[ 1.5 ms ] story [ 35.4 ms ] thread
Yes. There's so much effort to avoiding bashisms and learning additional languages (sed, awk) and many destros (particularly RHL/Fedora/RHEL, then later Ubuntu) settled on python almost 20 years ago now.
I have recently become convinced to use Python 3 scripts instead of all but trivial Bash scripts, however with one constraint: no libraries outside of the standard library for the minimum target platform, since requiring their installation is often non-trivial.
That's exactly what I do.

And it turns out the standard library has a lot of depth to it, even on not-really-python-friendly platforms like macos.

I have a standard sort of "template" program that uses argparse and just fill it in.

It's nice to be able to iterate over a list of files without worrying about files with spaces in the name. I read and write json and csv files and use curses to colorize output.

You can do all these things in a shell script, but they tend to get messy.

Why on earth would anyone choose Python as an improvement over bash when there are several mature dynamic languages which are much better suited to shell scripting? Ruby, Perl and Lua come to mind.

    Python is a concise more expressive language
No it is not, and that's why I'd put it last on the list. Kreist, Python doesn't even have baked-in regex support. And forget one-liners - Python just doesn't cut it. Nearly everything in Python has to be fully qualified which works against conciseness. Sure, you can import a function but it isn't idiomatic Python.

    It has a massive built-in library
So does Ruby which also has so many more methods built into the kernel that you don't end-up having to fully qualify everything, eg. random.random().
Even awk/perl is sometimes better than shell script in many use cases.
For scripts requiring any degree of complexity regarding data structures or architecture, for sure, use Python.

But like, you can't just use Python to interact with your system. It will be such a pain in the ass.

Piping and redirection in Bash is a million times more concise and expressive. Working with streams like this is what it excels at. But if you need to to populate some data structure and access data by key... python is better.

Python's import system requires multiple lines. Python's spacing requirements require multiple lines and awkward spacing when typing directly in the shell. Sometimes you are writing a script, and sometimes you just want to do ${action} to ${file}_${i} in the directory.

You owe it to yourself to be proficient in bash if you are working a lot in a unix-like environment.

I think a lot of your points could be addressed by using perl instead, and it's a lot more flexible in terms of manipulating text rather than using a combination of utilities like awk and cut.
Maybe! I've only ever glanced at it, but something between awk and python does sound useful.
When I have a lot of systems stuff combined with some algorithmic stuff, I'll often write out the latter in a python script and incorporate it into the bash script. Overall seems to work quite well, you can even put it in the same file to assist distribution
I often use jq for working with more complex data structures in bash, sure it's not built-in, so you're forced to extract some stuff into variables before interacting with it, but that often leads to cleaner code anyway.
I regularly use node for my scripts as it’s installed on all my machines and I know the language. Not saying you should choose node specifically, choose anything you’re comfortable with as the shebang [1] is a beautiful thing

[1] https://en.wikipedia.org/wiki/Shebang_(Unix)