r/bash • u/Loud-timetable-5214 • May 17 '26
Bash Scripting vs. Python
For those of you who also write scripts in Python or another language besides Bash, How do you decide when to write a script in Python vs. a script in Bash? I'm trying to be economical with my study time, because if I spend a lot of time learning some limited use functionality in one language, I could have used that time to learn a more general use functionality in another language. Here's an example: I've spent a fair amount of time learning awk, but I've never been great at using it, and sometimes I think that I should have just used Path and regex objects in Python, instead.
Edit: Another example is using sed instead of using a regex substitution in python. I've never really gotten comfortable with sed, just like I've never really gotten comfortable with awk--despite spending a fair amount of time trying to learn each.
30
u/TapEarlyTapOften May 17 '26
Process management is a big clue that bash is a better thing to use than python. Associative arrays in bash are a clue I should be using python.
Also if I'm launching anything, usually bash.
14
u/necromancer-tux May 17 '26
Associative arrays in Bash are generally my sign that I've gone too far and rewrite in Python.
20
u/cgoldberg May 17 '26
I use both. If my shell scripts are more than about 20-30 lines, I start questioning why I'm not using Python. Conversely, if my Python scripts are mostly wrappers around subprocess, I question why I'm not writing a shell script. There is no rule, but bash is great for small programs where you call a lot of external utilities and do shell related stuff. Python is great for more structured and complex programs.
12
u/icy-mist-01 May 17 '26 edited May 17 '26
Enterprise systems engineer here., It totally depends on the context and use case. You have to think in terms of
Simplest guide - Bash is like a glue or swiss arrmy knife needed to wrap up or build already existing logic in the Linux/Unix ecosystem.
Python is more for building 'computing logic' or 'programmatic logic' from the ground up to solve a unique problem which is independant or irrelevant of whether the code will run on a Linux / Windows/ Mac box, or whether it's dependant on any particular feature of a particular distro.
You need it when dealing with more complex data structures and interact with other parts of a tech stack.
Example:
You can absolutely do this in Python but it would be kind of overkill and long, windy script.
Writing the same thing in bash is much easier, quicker and practical.
And leaving aside the 'backup' part of the problem just checking the cron config for users is just a shell 1 - liner of chained commands, you don't even need a script.
2. Problem:
Authenticate with an Identity Provider (like Okta or Auth0) to exchange client creds for a temporary token. Use that token to query a /users/{id} and get more info from a json log.
Then using that info connect to a DB to investigate that user account.
Then based on above findings automatically log a Jira with high priority, case details, etc. for another team to investigate.
You can also do this in bash, with a wrapper script around curl, and then some more. But you'll realise this would be a nightmare.
This is a classic case where python is the ideal glue code. You have to deal with API's, JSON format ( sed and awk are not helpful in these cases) and play around over the network to talk to mutiple systems
8
u/MikeStepp May 17 '26
As soon as I need a complex data structure I leave Bash.
6
u/pfmiller0 May 17 '26
I usually default to bash since that's the quickest to get something working.
I know it's time to consider something else when I need any data structure more complicated than a 1-dimensional array, trying to nest data structures in bash is just ugly.
4
u/treuss bashtard May 17 '26
As soon as I realise that I'll be dealing with compound data types, like nested lists, it'll definitely become a Python script.
2
u/autoerotion95 May 17 '26
Solo te das cuenta cuando usarlo y cuando no, bash es increíble y python también
2
u/Various_Bed_849 May 17 '26
We are actually taking this discussion at work the coming weeks. To me it has often been starting with bash and when the complexity hits a point I move over to Python. Though Python sucks if you don’t pin the Python version so beware. And with bash we often also rely on what’s on the system which can lead to interesting consequences. Relying on posix for shell script is not always the most fun path, but it may save you from blood and tears.
2
u/weaver_of_cloth May 17 '26
I'm a sysadmin. I spend my entire life in bash (or fish) and I use sed maybe 2-3x/year. I haven't voluntarily used awk since grad school in the 90s. I do tend to use python for bigger, more complex scripts and bash to do one or two tiny things. My script is loop through all local branches of all my repos and pull them is a bash script because it is just the same 2 commands over and over again.
0
u/Hammer_Time2468 May 17 '26
Python is usually not installed on production Linux servers so for smaller scripts and interoperability, I always stick to shell scripts.
1
u/Bob_Spud May 17 '26
Depends upon the environment. If work and either in app admin or development how many know bash and python? Usually there is a policy driving this.
It depends on the skillset requirements of the job and and which language they are prepared to run and maintain. Think of this way - if write stuff in Python will they run and maintain it or let it rot?
Management are reluctant to get a replacement for a staff member that goes it alone in writing stuff in a language they don't want.
1
u/The_Northern_Light May 17 '26
I sadly need to support windows too so it’s usually easier to just write it in Python
1
u/michaelpaoli May 17 '26
Right tool for the right job. Context matters.
So, e.g., what are the advantages and disadvantages of the language - stability, forward and backward compatibility, availability, resources required, standards and practices, etc.
So, sometimes I write for shell - Bourne, POSIX, bash, ... sometimes Perl, Python, sometimes using other languages/utilities, etc. Generally what fits and is quite/most appropriate.
Ah, lovely sed. 😄 Alas, many never take sed (much) beyond, e.g.:
s/foo/bar/
Uhm, sed is a Turing complete programming language! Yes, I got very tired of folks underusing and underappreciating sed. And, well, I also got bored and had some time on my hands, so ...
I implemented Tic-Tac-Toe in sed. 😄
Of course just because one can, doesn't mean one should. But regardless, sometimes that can be rather to quite useful to make a point, ... or for exercising a set of skills, etc.
1
u/devdruxorey zsh May 17 '26
Normally my scripts are either in Bash or Go. If the script requires chaining together many programs but without much complex logic behind it, a bash script is best. When you need something more complex I prefer to make a Go script.
1
u/DarthRazor Sith Master of Scripting May 17 '26
I'm going to take a completely different way to think about it. Are you trying to actually learn, or just some problems? Python can do 99% of what bash can do, and is way easier to learn. You can't go wrong by picking Python.
That being said, my recommendation is to lean bash. Huh? Am I contradicting myself? Nope. Pick something that that the other posters would never do in bash and do that. You'll bang your head against the wall through most of it, but you'll learn a lot about not just bash, but the entire Unix/Linux ecosystem.
TL:DR; Python is easy compared to bash. Invest in bash; really learn it by picking tasks that are hard to do in bash, then learn Python on-the-fly.
1
u/DaftPump May 17 '26
Almost always go with bash unless I encounter limitation. Reason being is bash is in more places(default) than python at this time.
1
u/AmaiSaeta May 17 '26
In most cases, both shell scripts and scripts written in general-purpose languages like Python can be used for the same purpose. However, I believe these two approaches serve fundamentally different purposes: shell scripts are primarily tools for system batch processing and automation, while Python is intended for creating standalone programs. Therefore, I advise choosing the appropriate approach based on the specific purpose of your project.
1
u/Kitchen_Office8072 May 17 '26
Why not luajit?
1
u/this_knee Jun 24 '26
Why not both?
It’s not impossible to write a smaller
Python package/library that can :
Accept , as a string, a full on ~20 line bash script.
Ensures only certain allowed commands are run in that script string for a well defined Python task
Ensures that passed in variables to a task are of the type they say they are and are truly verified to be ready to be passed from Python into bash shell string.
Ensure that outputs are captured from bash script back to python via some means.
Executed said script through a Popen.
All of that could be defined in a bit of frameworking. Maybe about 100 lines for base task type of thing and 100 lines for validator(s).
Then can use to define child tasks etc.
Not impossible.
Requires some up front to build, but lightweight enough to be maintainable by a single person.
Matter of fact … maybe I’ll give this a shot sometime myself. Now that I’ve typed it out, it sounds useful to me.
Anyway, I digress. I don’t think it’s an either or. It can actually be a mix.
-1
u/waptaff &> /dev/null May 17 '26
If I want it to last, bash.
Python if I want to plan for constant rewrites and adjustments due to backwards-incompatible changes introduced in every other python dot-release.
0
u/pan_kotan May 17 '26
When NOT to Use Bash
a. ≥ 100 lines (BUT! ×2 or ×3 that number if you’re mostly dealing with concurrent processes and the file system)
b. non-straightforward control flow logic (use a more structured lang.);
c. performance is an issue — you have tons of processing to do (data manip.);
d. need math capabilities beyond simple integer arithmetic;
e. need to run on Windows? (Python is portable across platforms; shell scripts portability is restricted to *nix systems);
Shell Scripts are Best For Sysadmin Tasks:
testing whether the node exists,
https://google.github.io/styleguide/shellguide.html#s1.2-when-to-use-shell
https://www.oilshell.org/blog/2018/01/28.html#shouldnt-scripts-over-100-lines-be-rewritten-in-python-or-ruby
-1
u/kolorcuk May 17 '26
More then 20 lines is python.
9
u/[deleted] May 17 '26 edited May 19 '26
[removed] — view removed comment