Shell Scripting vs Python
Debate on whether to use shell scripts (like Bash) or general-purpose languages like Python and Ruby for automation, scripting, and complex tasks, weighing pros like ubiquity and simplicity against cons like poor scalability and maintainability.
Activity Over Time
Top Contributors
Keywords
Sample Comments
Shell scripts don't scale and are hard to maintain. Literally any programming language would be a better choice for the same task.
Are there ever good reasons to choose a shell script over a Python/Ruby/etc script?
I've also wanted a better shell scripting experience, and have bashed [no pun intended] my head against this several times. I think some of the major pain points that resist adoption of a language like Python or Ruby for in-line shell scripting or simple automation is:* These languages prefer to operate on structured data. If it parses from json, you have a much easier time. But, most commands you'd invoke from the shell emit unstructured text by default. You can deal with this, but
Use the shell only if your script is mostly about calling other programs and filtering and redirecting their output. That's what the syntax of these languages is optimised for. As soon as you need any data manipulation (i.e. arrays, computation, etc.) it becomes a pain and Python is the much better fit.
Pro shell scripts:- I often encounter machines without Python or Go. I never encounter machines without Bash.- Python and Go will probably change faster than Bash. So the maintenance cost of scripts in Python or Go will be higher.- When you figured out something tricky on the command line, you can paste it into a Bash script verbatim.- Bash is the perfect glue language. If the task at hand is to glue together some commands, nothing beats Bash.
So it's not a fundamentally different use-case, it's just an admission that shell scripts suck at what they do?
Why are you against shell scripting languages ?
Python is available virtually anywhere. I'd also not use Bash unless it's a very restricted situation. Every time I'm writing a shell script that's more than a page, a sense of imminent doom starts to dominate my soul.
When it's more than 2-3 lines of shell script it's better to just use Python (used to be Perl).
Well this doesn't make much sense.If you rightfully find bash inadequate at some point, just switch to the next most natural tool - Python. Nobody's going to be surprised when they see a Python script.If you start feeling Python is inadequate too, then:1) You're wrong, just stick to Python2) If you're so bent on doing away with familiarity, at least get as much leverage as you can in return and use a "proper" language, like Go / Rust. You get static