shell script
execute python
bash script
python integration
command line

Shell Script Execute a python program from within a shell script

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Running a Python script from a shell script is a practical pattern when Bash handles orchestration and Python handles data processing. The integration is simple, but reliability depends on explicit interpreter selection, argument handling, and exit code management. A clean interface between the two layers prevents silent failures in automation jobs.

Core Sections

Launch Python explicitly from Bash

Use an explicit interpreter and strict shell flags so the script stops on failure. This removes ambiguity across machines where python may point to different versions.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
5PYTHON_BIN="${PYTHON_BIN:-python3}"
6
7"$PYTHON_BIN" "$SCRIPT_DIR/process_data.py" --input "$SCRIPT_DIR/input.json"

This approach makes local and CI behavior consistent. If your environment requires a specific runtime, pass PYTHON_BIN from deployment configuration instead of hardcoding a path in every script.

Pass arguments and capture exit status

Bash should pass parameters to Python without reshaping user input. Quote every variable and check the process status so failures are visible to callers.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4file_path="$1"
5python3 ./validate_file.py "$file_path"
6status=$?
7
8if [ "$status" -ne 0 ]; then
9  echo "Validation failed for $file_path" >&2
10  exit "$status"
11fi
12
13echo "Validation succeeded"
python
1# validate_file.py
2import argparse
3from pathlib import Path
4
5parser = argparse.ArgumentParser()
6parser.add_argument('file_path')
7args = parser.parse_args()
8
9path = Path(args.file_path)
10if not path.exists() or path.stat().st_size == 0:
11    raise SystemExit(2)
12
13print('ok')

The Python script returns a nonzero status when validation fails. Bash then forwards that status to upstream tooling, which is essential for CI pipelines and cron monitoring.

Use virtual environments for reproducibility

A common source of production drift is running the right script with the wrong dependency set. Create and activate a virtual environment in the shell wrapper so package versions remain predictable.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4python3 -m venv .venv
5source .venv/bin/activate
6pip install -r requirements.txt
7python app_task.py

If startup time matters, create the environment once during provisioning and keep runtime scripts focused on execution only. Rebuilding dependencies on every run is safe but can be slow for frequent jobs.

Verification and operational checks

After implementing the fix, verify behavior with a short, repeatable check list. Confirm the happy path first, then test malformed input, missing dependencies, and permission boundaries. This sequence catches most regressions before they reach production.

When the workflow is part of automation, log inputs and outputs at a useful level. Structured logs with request identifiers make failures easier to trace and reduce debugging time during incidents. Keep the runbook close to the code so updates remain synchronized with implementation changes.

Practical rollout pattern

A reliable way to ship this pattern is to introduce one small change, measure behavior, then expand scope. Start with a constrained environment such as a local test dataset or one noncritical endpoint. Confirm logs, metrics, and error messages are understandable by someone who did not author the change. That validation step is where many teams discover unclear assumptions.

After confidence is established, document the final operating procedure in concise steps. Include exact commands, expected outputs, and a short recovery plan for common failures. Clear operational guidance reduces repeated investigation work and shortens incident response time. It also makes onboarding easier because new contributors can follow a known path instead of inferring hidden workflow details from scattered code comments.

Common Pitfalls

  • Calling python instead of python3 and hitting version mismatch issues.
  • Forgetting quotes around shell variables and breaking paths with spaces.
  • Ignoring Python exit codes so failed jobs appear successful.
  • Activating a virtual environment in one shell and executing in another shell.
  • Mixing orchestration logic and heavy data logic in one large Bash file.

Summary

  • Use explicit interpreter selection and strict Bash flags.
  • Quote arguments and propagate Python exit codes.
  • Keep Python scripts responsible for business logic and validation.
  • Use virtual environments to avoid dependency drift.
  • Add repeatable verification checks for automation reliability.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.