Python and Bash for DevOps: What to Learn First
By Branko Petric - Updated - About 5 weeks at 10 hours a week
The short answer
For DevOps, learn Bash first, then enough Python to write small tools. You should be able to write a safe Bash backup script and a Python script that calls an API, parses JSON and handles errors. You do not need algorithms or web frameworks. Plan on about 5 weeks at 10 hours a week.
Why scripting comes after Linux and Git
By now, you have typed the same commands many times. That repetition tells you what to automate. It also means you already know what each command should do, so you can focus on the script.
Scripts also fail in ways you must plan for. A script that keeps going after an error can do real damage on a server. Your Git repo gives you a safe place to change scripts and undo mistakes. You will also reuse what you build here at step 5, where your backups need a script.
What to learn, in order
Spend about 2 weeks on the Bash items, then about 3 on Python.
Bash
- Variables and quoting. Write
"$name", not$name. Without quotes, a file name with a space turns into two arguments. That can makermdelete the wrong thing. - Arguments. Learn
$1,$@and$#, so one script can work on any folder or server you give it. - Exit codes. Every command returns a number, and 0 means success. Check the last one with
$?. CI tools use this number to decide whether a job failed. Cron does not act on it, so your script must report its own failures. - Tests and loops. Learn
if,for, and file checks such as[ -d "$dir" ]. - Safe defaults. Start scripts with
set -euo pipefail. It stops on most command failures, treats unset variables as errors, and catches failures inside pipes. It has exceptions, so still check the steps that matter. - Text tools. Learn
grep,sedandawkto pull values out of logs and command output. Learn just enough to handle common cases. - Scheduling. Learn cron (the Linux tool that runs commands on a schedule). Cron hides output by default on many servers, so send it to a log file.
- Checking. Run ShellCheck on every script. It finds quoting bugs and other errors that are easy to miss.
Python
- The core language. Start with the official tutorial. You need variables, lists, dictionaries, loops, functions and reading files. Dictionaries matter most, because JSON loads into them.
- Virtual environments. A virtual environment gives one project its own packages. Create one with
python3 -m venv .venv. On Ubuntu, you may first needsudo apt install python3-venv. - Calling APIs. Use the Requests library to call REST APIs (web addresses that programs use to send and receive data). This is where Python starts to beat Bash.
- JSON. The
jsonmodule turns text into dictionaries and lists. Then you can read a nested value with a line likedata["items"][0]["status"]. - Error handling and exit codes. Learn
tryandexcept, then usesys.exit(1)when a task fails. Without this, a failed tool can look like a success to CI or to a script that calls it. - Running commands.
subprocess.runcalls other programs. Passcheck=True, so a failed command raises an error instead of being ignored. - Files and arguments.
pathlibhandles file paths.argparsegives your tool real options and a--helpmessage. - One test. Python includes
unittest. Test the part that makes a decision, such as "is this certificate close to expiry?", without making real network calls.
Bash vs Python: which to use
Use this rule of thumb, and expect some overlap:
- Use Bash when the job is mostly running other commands in order. A backup, a log cleanup and a deploy helper are good fits.
- Use Python when the job works with data. Calling an API, reading JSON, retrying on failure and writing tests are all easier there.
- Switch from Bash to Python when you need arrays of records, nested logic or tests. Those are signs that the Bash script has outgrown its job.
Here is the same idea of "fail loudly" in each. This Bash script is mostly running commands:
#!/usr/bin/env bash
set -euo pipefail
source_dir="$1"
backup_dir="$2"
if [ ! -d "$source_dir" ]; then
echo "Folder not found: $source_dir" >&2
exit 1
fi
backup_file="$backup_dir/backup-$(date +%F).tar.gz"
tar -czf "$backup_file" "$source_dir"
echo "Backup written: $backup_file"
This Python script checks a web reply, so it needs real error handling. Run pip install requests inside your virtual environment first:
import sys
import requests
url = sys.argv[1]
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
except requests.RequestException as error:
print(f"FAIL {url}: {error}")
sys.exit(1)
print(f"OK {url}: {response.status_code}")
What to skip for now
- Object-oriented design and design patterns.
- Web frameworks such as Django and Flask, beyond what the small API at step 5 needs.
- Algorithm drills and LeetCode. They rarely help you automate a server.
- Go and other extra languages.
- Long comparisons of Python tools.
venv,pipandunittestare enough.
Common mistakes
- Using
pip installsystem-wide. Recent Ubuntu versions block this with anexternally-managed-environmenterror, as described in PEP 668. Do not force it. Use a virtual environment. - Calling an API without a timeout. Requests waits with no time limit unless you set one. A hung server can then hang your cron job too.
- Catching every error and printing it. A script that prints a message and exits with 0 reports success. Exit with a non-zero code when the job failed.
- Putting passwords in scripts. Your scripts are in Git, so read secrets from environment variables instead.
- Writing a 300-line Bash script. If you are building data structures in Bash, move to Python.
- Using Python for everything. Three commands in a row do not need a Python program. A short Bash script is easier to read.
Your project
Build a health-check tool in Python that takes a list of URLs. For each one, it should:
- Request the URL and report the status code. Use a timeout.
- Report how many days are left on the TLS certificate. The
sslmodule can read this. - Check free disk space with
shutil.disk_usage. - Exit with a non-zero code if any check fails.
Add one test, and keep the tool in your Git repo through a pull request. Separately, write the Bash backup script above and run it from cron. Make it send you a message when it fails, by email or a chat webhook (a URL that posts a message to a chat room).
You will use both again: the backup script at step 5, and the health check once you reach observability.
You are done when
You can do all of this with no notes. If you already can, skip this step.
- You write a Bash backup script that takes a folder as an argument, uses
set -euo pipefail, and passesshellcheckwith no warnings. - You point that script at a folder that does not exist. It prints an error and
echo $?shows a non-zero exit code. - You write a Python script that calls a REST API with a timeout, parses the JSON reply, and exits with code 1 if the request fails.
- You install that script's packages in a virtual environment, and it has at least one passing test run with
python3 -m unittest. - You can explain why you chose Bash or Python for each tool you wrote.
Learn it and test it
Free guides
Practice scenarios
- Nightly Backups Run. Are You Actually Covered?
Automated backups have run for months. A teammate says you are fully covered for disasters. Defend whether you agree.
- The Cert Expires Tonight. Just Bump It to a 10-Year Cert?
Your nginx TLS cert expires tonight and an expired cert took the site down last quarter. A teammate wants a 10-year self-signed cert. Defend the real fix.
Common questions
Should I learn Python or Bash first for DevOps?
Learn Bash first. It is short, you already use its commands from the Linux step, and it runs on nearly every server. Move to Python when your scripts need to handle API replies, complex data or tests. You will end up using both.
Does a DevOps engineer need to know how to code?
You need to read code and write small tools, but you do not need to work like a software engineer. You should be able to automate a task, handle its errors and follow an app's code well enough to debug a failed deploy.
Should I learn Go for DevOps?
Not yet. Tools such as Kubernetes and Terraform are written in Go, so it can help if you later write tools that extend them. You can use those tools very well without it. Learn it on the job if your team needs it.
How much Python do I need for DevOps?
Enough to pass the test on this page. That means variables, loops, functions, lists, dictionaries, reading files, calling an API, catching errors and writing a basic test. You can skip classes for now, since most small tools work fine without them.
Practice defending your decisions
DevOps Ledger is a scenario-based DevOps and SRE interview practice platform: you pick a real production incident, defend your decision in a chat against a senior-engineer AI that pushes back, and get scored against an answer key written before you started.
One payment. 12 months of access. No subscription, nothing renews.
Get full access - $19