Turn commands you type once into automation you run forever.
Module 3 · Variables, conditionals, loops, functions — build a real script. Free · Local.
Beginner Foundations ~50 minchmod +x, ./script.sh)if and loops with for / whilePrerequisites: Module 2 (Linux) — you should be comfortable with basic commands.
A Bash script is just a text file full of the same commands you already type — saved so you can run them all at once, every time, identically. That's the very definition of automation, and it's where DevOps begins.
Anything you do more than twice in the terminal is a candidate for a script: backups, deployments, log cleanup, environment setup. Scripts don't get tired, don't forget a step, and run in seconds.
Dockerfiles run shell commands. CI/CD pipelines (Module 9) are largely shell steps. Server setup is shell. Learning Bash now pays off in every later module.
Always wrap variables in double quotes: "$name". Without quotes, a value with spaces breaks your script. This single habit prevents most beginner Bash bugs.
We'll build up a real, useful script piece by piece: it backs up a folder into a timestamped archive. Create a file backup.sh and follow along.
Make it executable and run it:
set -euo pipefail makes scripts fail fast instead of silently continuing after an error — a professional habit.
ifCheck the source exists before backing up; create the destination if missing.
-d dir exists · -f file exists · -z string empty · -eq/-lt/-gt number equals/less/greater. Negate with !.
Run ./backup.sh — you now have a timestamped archive in ~/backups. Run it again; you get a second one. That's automation.
A for loop repeats work. Here's how you'd list each .tar.gz backup with its size:
$1, $2… are the arguments passed to your script. Let the user choose the folder:
$1 $2 args · $0 script name · $# number of args · $@ all args · $? exit code of last command.
Functions keep scripts tidy and reusable:
Variables, a safety header, a conditional, a loop, arguments, and a function — the same constructs power deployment and setup scripts everywhere in DevOps.
Everything assembled:
| Syntax | What it does |
|---|---|
#!/usr/bin/env bash | Shebang — first line of every script |
set -euo pipefail | Fail fast on errors (use it always) |
name="value" | Set a variable (no spaces around =) |
"$name" | Read a variable (always quote it) |
$(command) | Capture a command's output |
if [ ... ]; then ... fi | Conditional |
for x in ...; do ... done | Loop over items |
while [ ... ]; do ... done | Loop while true |
name() { ... } | Define a function |
$1 $@ $# $? | Args, all args, count, last exit code |
cmd1 && cmd2 | Run cmd2 only if cmd1 succeeds |
cmd > file 2>&1 | Redirect output + errors to a file |
| Symptom | Likely cause & fix |
|---|---|
Permission denied on run | Not executable — chmod +x script.sh. |
command not found: ./script.sh | Run with ./ from its folder, or bash script.sh. |
| Variable empty unexpectedly | Typo, or you wrote name = value with spaces. Remove the spaces. |
| Breaks on filenames with spaces | Quote every variable: "$file". |
| Script keeps going after an error | Add set -euo pipefail at the top. |
| Unsure why it fails | Run bash -x script.sh to trace each line. |
find "$backup_dir" -name '*.tar.gz' -mtime +7 -delete).--help flag that prints how to use the script.Write executable Bash scripts with variables, conditionals, loops, arguments, and functions — and you built a real backup tool. This is the automation mindset DevOps runs on.
Next up: Module 4 — Git & GitHub, where you'll track changes to code (including your scripts) and collaborate — and create the repo you'll use for the rest of the course.