Bash vs Python vs Go
"Which language should this be in?" comes up for every new piece of automation, and the wrong answer is expensive in both directions: a 900-line Bash script nobody dares touch, or a Go module with a build pipeline for something that should have been four lines in a CI step. There is no universal answer, but there are reliable signals.
The signals
| Signal | Points to |
|---|---|
| mostly runs other commands in sequence, little logic | Bash |
runs inside a CI step, Dockerfile RUN, container entrypoint, Makefile recipe |
Bash (or POSIX sh in minimal images) |
parses JSON/YAML beyond a jq one-liner, or needs dictionaries, sets, classes |
Python |
| talks to an HTTP API with auth, pagination and retries | Python (or Go) |
| needs unit tests, or will be maintained by several people | Python or Go |
| distributed to many machines or other teams, must not depend on an interpreter version | Go |
| long-running (agent, exporter, operator, webhook) | Go |
| high concurrency: hundreds of parallel checks, streams | Go |
| a cloud SDK or library only exists in one language (e.g. a vendor's Python SDK) | that language |
| the team already maintains tools in one language | that language, unless a signal above is strong |
Two cross-cutting questions settle most ties:
- Who runs it, and where? A script only you run on one server can use whatever is installed there. A tool for thirty colleagues on laptops with different operating systems should be one binary.
- How long will it live? A one-off migration can be quick and ugly. Something that will run every night for three years deserves types and tests.
A rule-of-thumb model
The rules above, written down as a function and applied to typical tasks. It is deliberately simple: the point is to make the reasoning explicit and discussable in a review, not to automate the decision.
def recommend(task):
t = task
if t.get("distributed") or t.get("long_running") or t.get("parallel", 0) > 50:
reason = ("ships to many machines" if t.get("distributed") else
"long-running service" if t.get("long_running") else "high concurrency")
return "Go", reason
if t.get("api") or t.get("data_logic") or t.get("tests") or t.get("lines", 0) > 150:
reason = ("HTTP API work" if t.get("api") else
"real data structures" if t.get("data_logic") else
"needs tests" if t.get("tests") else "too big for Bash")
return "Python", reason
return "Bash", "mostly running commands"
tasks = [
("CI step: build image, tag, push", {"lines": 12}),
("container entrypoint: wait for DB, run migrate", {"lines": 25}),
("nightly: find stale feature branches via API", {"api": True, "lines": 120}),
("merge env overrides into 40 Helm values files", {"data_logic": True, "lines": 80}),
("CLI for 30 devs: request temporary DB access", {"api": True, "distributed": True}),
("exporter: expose queue depth as metrics", {"long_running": True}),
("check TLS expiry of 400 endpoints", {"parallel": 400, "lines": 60}),
("log rotation helper on one server", {"lines": 30}),
("Bash deploy script that grew to 600 lines", {"lines": 600, "tests": True}),
]
for name, task in tasks:
lang, why = recommend(task)
print(f"{lang:6} <- {name} ({why})")Bash <- CI step: build image, tag, push (mostly running commands)
Bash <- container entrypoint: wait for DB, run migrate (mostly running commands)
Python <- nightly: find stale feature branches via API (HTTP API work)
Python <- merge env overrides into 40 Helm values files (real data structures)
Go <- CLI for 30 devs: request temporary DB access (ships to many machines)
Go <- exporter: expose queue depth as metrics (long-running service)
Go <- check TLS expiry of 400 endpoints (high concurrency)
Bash <- log rotation helper on one server (mostly running commands)
Python <- Bash deploy script that grew to 600 lines (needs tests)
You could argue several rows: a 400-endpoint TLS check is also fine in Python with a thread pool, and an exporter can
be Python with prometheus_client. That is the point of writing the reasoning down.
Signs a Bash script should be rewritten
- It parses JSON or YAML with
grep,sedorawk. - It has arrays of "records" (
name_1,port_1, …) or associative arrays pretending to be objects. - It is over a couple of hundred lines, or has more than a handful of functions.
- Nobody can say what it does on error, or people are afraid to run it.
- It needs tests, and you have started looking at test frameworks for Bash.
- It is copied between repositories with small edits each time.
The rewrite usually goes to Python first. Go becomes attractive when the Python version starts needing a
requirements.txt on every machine that runs it, or when it turns into a service.
Signs you over-engineered it
- A Go module, a Dockerfile and a release pipeline for a script with no logic.
- A Python package with
setupboilerplate to runkubectl rollout restarton three deployments. - Wrapping every shell command in
subprocess.runwith no logic in between. Bash is the better language for "run these commands in order".
POSIX sh vs Bash
Minimal images (Alpine's BusyBox, distroless with a shell layer, debian:*-slim's dash as /bin/sh) do not
always have Bash. A script that must run there should be POSIX sh: no arrays, no [[ ]], no local in strict
POSIX (though dash and BusyBox support it), no pipefail in older shells (POSIX added it in the 2024 edition).
ShellCheck checks the dialect named in the shebang, so #!/bin/sh scripts get POSIX warnings automatically.
Test yourself
A tool will be used by 30 developers on macOS, Linux and Windows to request temporary credentials from an internal API. Which language fits best?
- Bash
- Python
- Go
- Any of them equally
Which is the clearest sign that a Bash script should become Python?
- It uses
set -euo pipefail - It parses JSON with grep and sed and keeps records in numbered variables
- It runs in CI
- It calls docker build
What is the best language for a container entrypoint that waits for a database and then execs the app?
- Go, for speed
- Python, for readability
- A short shell script (Bash or POSIX sh)
- Java