Foreword
Read a little, type a lot. Sooner or later, everyone who looks after servers writes their first shell script: a backup, a health check, a few lines that search through a log. And sooner or later, one of them breaks in the middle of the night – because of a space in a file name, a missing variable, or because cron has a different environment than your terminal.
This book makes you confident in Bash. You learn the language from
the ground up: exit codes, quoting, conditions, loops, functions and
arrays. Then the text tools that shell scripts use to do their work:
grep, sed, awk, curl
and jq. After that, it is about scripts you can rely on:
error handling with trap, locks, tests with bats, and
ShellCheck as your constant companion. At the end, your scripts run in
operations: in the background, with signals, via cron and systemd
timers, and on several servers over ssh.
The common thread is a toolbox for the notes application, a small notes API with Postgres and nginx in front of it. It grows day by day: a health check, a report from the access log, a backup with rotation, a watchdog and a deploy helper – all of them tested.
This book is for admins, developers and ops people who know the command line and want to write scripts that hold up at night, too.
The training rules
- Type, don’t copy. Every command once by yourself.
- Compare with Expected. After each drill, you see what you should get. If yours differs, that is where the real lesson is.
- Pass the checkpoint. Only move on once the check commands show what is written there.
- Solve the challenge on your own. First the hint, then the solution.
- ShellCheck is always there. Every script goes
through
shellcheckbefore it runs for the first time. - Lab values stay in the lab.
lab-api-key-123andlab-db-pw-123are practice data.
Warning: All output comes from real test runs. Times, IDs and IP addresses will differ on your machine. What matters is what happens.
Structure of each training day
| Section | Purpose |
|---|---|
| Briefing | The essentials in a few bullet points |
| Drills | Commands and scripts with expected output |
| Checkpoint | Individual check commands: did everything work? |
| Challenge | A task without instructions |
| Enterprise Note | What is different in production |
Boxes marked POSIX show where Bash differs from the
portable sh – important when a script also has to run under
dash, BusyBox or in an Alpine container.
The training plan
| Part | Day | Unit |
|---|---|---|
| 1 – Language | 0 | The training ground |
| 1 | The first script | |
| 2 | Variables, quoting, parameter expansion | |
| 3 | Conditions and loops | |
| 4 | Functions and a library | |
| 5 | Arrays and options | |
| 2 – Text | 6 | grep, cut, sort, uniq |
| 7 | sed and awk | |
| 8 | curl and jq | |
| 9 | find, xargs, mktemp | |
| 3 – Robust and tested | 10 | Error handling, locks, backup |
| 11 | ShellCheck, shfmt, bats | |
| 4 – Operations | 12 | Processes and signals |
| 13 | cron and systemd timers | |
| 14 | Several servers with ssh and rsync | |
| Competition | 15 | Disaster drills |
| Appendix | A | Cheat Sheet |
| B | Final Exam |
The lab
You work over SSH on an Ubuntu 26.04 VM sh with 2 CPUs
and 4 GB RAM, which you start with Multipass. For Day 14, a small second
VM ziel joins it. Any other Linux VM with Bash 5,
sudo, Docker and internet access works too.
The tested versions: Bash 5.3, ShellCheck 0.11.0, shfmt 3.14.1, bats-core 1.14.0, jq 1.8, Docker 29.9 with Compose 5.6, Postgres 18.6 and nginx 1.31.
You can find the companion code at
https://github.com/thomaszachmann/shell-scripting-zum-anfassen;
Day 0 fetches it with git clone. The loesung
folder contains the finished toolbox after Appendix B – for comparing,
not for copying.
Where this book only touches on a topic, the sister books go into depth: “The Mac terminal hands-on” for zsh and the terminal itself, “Docker Compose hands-on” for the notes stack, “Container tools hands-on” for ShellCheck and hadolint in the image pipeline.
Let’s go.