Avoiding Bash frustration – Use Python for shell scripts
davidoha.medium.com
davidoha.medium.com
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.
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.
E.g.
from marcel.api import *
for file, size in (ls('marcel',
'test',
file=True,
recursive=True) |
map(lambda f: (f.path, f.size))):
print(f'{file}: {size}') 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().