Yeda AI Tips · #176

Español

Any Bash Script Past 200 Lines Belongs in Python

Any bash script past two hundred lines with conditionals should be rewritten in Python. Shorter, and finally testable.

Bash is superb glue for a dozen lines: run a command, check the exit code, pipe to the next thing. But it was never designed to be a programming language, and past a couple hundred lines with real branching it stops being glue and starts being a liability that no one on the team can safely change.

What bash doesn't have

The problems aren't stylistic — they're structural gaps:

A large shell script is one off-by-one quote away from rm -rf on the wrong path, and because it can't be unit-tested, no one refactors it. It calcifies.

The rewrite pays for itself

The same logic in Python is typically shorter, not longer, because you stop fighting the language:

import subprocess, sys

def run(cmd: list[str]) -> str:
    result = subprocess.run(cmd, capture_output=True, text=True)
    if result.returncode != 0:
        raise RuntimeError(f"{cmd[0]} failed: {result.stderr.strip()}")
    return result.stdout

def deploy(env: str) -> None:
    if env not in ("staging", "production"):
        raise ValueError(f"unknown env: {env}")
    artifacts = run(["build", "--env", env]).splitlines()
    ...

A ~200-line bash script often becomes ~150 lines of Python with real exceptions, typed data, and functions you can import and test. And subprocess arguments are passed as a list, so there's no shell word-splitting or quoting minefield.

ConcernBashPython (or Go/Ruby)
Data structuresStrings + arraysLists, dicts, dataclasses
Error handlingExit codes, set -e trapsExceptions, stack traces
Unit testsNone practicalpytest, mocks, fixtures
Refactoring safetyManual, fragileTests + type checker

Where the line is

This isn't "never use bash." Short, linear glue stays in bash. The trigger is length plus branching: once a script pushes past roughly 200 lines and carries meaningful conditionals, loops, and error paths, the cost of bash's missing features exceeds the cost of a rewrite. On a deploy or data-migration path, a test suite is the difference between confidence and crossed fingers.

Power moves

Resources

Shipping AI or cloud systems? Yeda AI audits and hardens production LLM and deployment pipelines. Talk to us · Read the blog