Every DevOps team has a folder of shell scripts nobody talks about. One rotates logs, one checks that a service came back after a deploy, one cleans up old Docker images before the disk fills. They are not glamorous, but when something breaks at 2 a.m., they are often what keeps it running.
The short answer: Bash scripting means writing a file of shell commands that runs as one program. For DevOps you need variables, exit codes, if tests, loops, functions, and a few safety settings such as set -euo pipefail. With those you can automate server checks, backups, deploy steps and CI pipeline jobs. Most people get useful at it in two to three weeks of daily practice.
Shell scripting sits right after Linux basics in most learning plans, including our DevOps training in Pune. This guide covers the parts you will actually use, with small scripts you can run on any Linux machine.
Why Bash still matters when Python exists
Bash is already installed on almost every Linux server, container base image and CI runner. Nothing needs to be added before a script runs. It is also the natural language for gluing other commands together: run a tool, check whether it worked, pass its output to the next tool.
Python is better once a task needs real data structures, error handling across many steps, or API calls. A rough rule: if the script is mostly running other commands and is under about a hundred lines, Bash is fine. If it is mostly logic, reach for Python. Our guide to Python for DevOps picks up where this one stops.
Your first script
Create a file called hello.sh, make it executable with chmod +x hello.sh, and run it with ./hello.sh. The first line, called the shebang, tells Linux which program should run the file.
#!/usr/bin/env bash
set -euo pipefail
name="${1:-world}"
echo "Hello, $name. Today is $(date +%A)."
Three ideas are already in there. $1 is the first argument passed to the script, and ${1:-world} uses “world” if none was given. $(date +%A) runs a command and drops its output into the text. And set -euo pipefail makes the script stop on errors instead of carrying on blindly, which is covered below.
The building blocks you will use every day
Variables and quoting
Assign with no spaces around the equals sign: app="payments". Read with $app. Always put double quotes around variables when you use them, as in "$app". Without quotes, a value with a space in it splits into two words, and that is behind a large share of real-world script bugs.
Exit codes
Every command returns a number when it finishes. Zero means success and anything else means failure. $? holds the code of the last command. This is how scripts, and CI tools such as Jenkins or GitHub Actions, decide whether a step passed. A script that ends with exit 1 will fail a pipeline stage.
Tests with if
Use [[ ]] for checks. -f tests that a file exists, -d a directory, -z an empty string, and -gt a number greater than another. You can also test a command directly: if grep -q ERROR app.log; then runs the block only when grep finds a match.
Loops
A for loop repeats over a list, which is handy for running the same check on several servers or files. A while read loop processes a file one line at a time, which is the usual way to work through a list of hostnames or a CSV export.
Functions
Group repeated steps into a function, such as log() { echo "$(date +%T) $*"; }, and call it like any command. Functions keep longer scripts readable and make it obvious what each part does.
A real example: a service health check
Here is the kind of script you will find on many teams. It checks a list of URLs, prints the result for each, and exits with a failure code if any are down, so it can run from cron or as a step after a deploy.
#!/usr/bin/env bash
set -euo pipefail
urls=(
"https://example.com/health"
"https://example.com/api/status"
)
failed=0
log() { echo "$(date +%T) $*"; }
for url in "${urls[@]}"; do
code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$url" || echo "000")
if [[ "$code" == "200" ]]; then
log "OK $url"
else
log "FAIL $url (HTTP $code)"
failed=1
fi
done
exit "$failed"
Read it line by line and every piece from the earlier sections appears: an array of URLs, a function, a loop, command substitution, a test and an exit code. The || echo "000" catches a curl timeout so that set -e does not end the script before it has checked every URL.
In a pipeline, the same script becomes a smoke test that runs right after deployment. If it exits with 1, the stage goes red and the team knows before users do. Our explainer on how a CI/CD pipeline works shows where that step fits.
Safety settings worth memorising
set -estops the script when a command fails, instead of running the next line on bad data.set -utreats an unset variable as an error, which catches typos such as$APP_DIRversus$APPDIR.set -o pipefailmakes a pipeline fail if any command in it fails, not just the last one.trapruns clean-up code, such as deleting a temporary file, even when the script exits early.
Then install ShellCheck and run it on every script you write. It points out unquoted variables, risky patterns and common mistakes, and it explains each warning. Reading its output for a few weeks teaches you a great deal.
Habits that separate a good script from a fragile one
Print what the script is doing, so whoever reads the log later can follow it. Check that the tools you need exist before using them, for example with command -v docker. Never hard-code passwords; read them from environment variables that your CI tool or secrets manager provides.
And know when to stop. If a script starts managing the state of many servers, a configuration tool is usually a better fit. Our guide to Ansible for beginners covers that next step, and many teams run Ansible and small Bash scripts side by side.
A two-week practice plan
- Days 1 to 3: write five small scripts using variables, arguments and
iftests, such as one that warns when a disk is over 80 percent full. - Days 4 to 7: add loops and functions. Process a log file and count errors by hour.
- Days 8 to 10: build the health check above, schedule it with cron, and make it write to a log file.
- Days 11 to 14: run ShellCheck on everything, fix each warning, and use one script as a step in a GitHub Actions workflow.
If commands such as grep, awk or file permissions still feel shaky, spend a few days on Linux for DevOps first. Scripting is much easier when the commands inside the script are already familiar.
Frequently asked questions
Is Bash scripting required for DevOps jobs?
For most roles, yes. Interviewers often ask candidates to read or write a short script, and day-to-day work regularly involves editing shell steps in pipelines, Dockerfiles and server setup scripts.
What is the difference between Bash and shell scripting?
Shell scripting is the general term for scripts run by a Unix shell. Bash is the most common shell on Linux, so in practice the two terms are usually used to mean the same thing.
Should I learn Bash or Python first?
Learn basic Bash first, because you need the command line for everything else. Move on to Python once you are comfortable writing scripts with loops, tests and functions.
Can I practise Bash on Windows?
Yes. Install WSL (Windows Subsystem for Linux) to get a real Linux environment, or use Git Bash for lighter practice. A free-tier cloud VM also works well.





