Skills

Shell scripting

Turning the commands from the Linux skill into Bash scripts that run unattended, handle bad input and fail safely.

Intermediate · 13 topics · 4 projects · about 3 hours of reading

Step 3 of the DevOps engineer roadmap

Start with Shell scripting basics

Topics

  1. 01

    Shell scripting basics

    What a script is, the shebang line, making a script executable, and running it.

    7 min
  2. 02

    Shell variables

    Setting and reading variables, quoting, command substitution, and environment variables in scripts.

    9 min
  3. 03

    Conditionals

    if, test and [[ ]], comparing strings, numbers and files, and case statements.

    11 min
  4. 04

    Loops

    for, while and until loops, reading a file line by line, and break and continue.

    11 min
  5. 05

    Parameter expansion

    Default values, trimming and replacing parts of a value, and lengths, without calling other programs.

    9 min
  6. 06

    Arguments and input

    Positional arguments, shift, options with getopts, reading input, and a usage message.

    12 min
  7. 07

    Functions

    Defining functions, arguments and return values, local variables, and splitting a script into parts.

    10 min
  8. 08

    Arrays

    Indexed and associative arrays, looping over them, and keeping file names with spaces intact.

    11 min
  9. 09

    Arithmetic and strings

    Integer maths with $(( )), comparing and formatting numbers and text, and printf.

    12 min
  10. 10

    Exit codes and errors

    Exit statuses, checking them, && and ||, writing errors to stderr, and exiting with the right code.

    11 min
  11. 11

    Robust scripts

    set -euo pipefail, traps and clean-up, temporary files, lock files, and scripts that are safe to run twice.

    12 min
  12. 12

    Debugging

    Tracing a script with set -x, ShellCheck, and finding the line where it goes wrong.

    13 min
  13. 13

    Real-world scripts

    Patterns from scripts that run in production: logging, configuration files, dry runs, and running from cron.

    15 min

Projects

Do these after the topics. Each one runs on your own computer.

Project 1 · 9 min read

Automate a backup

Write a script that archives a directory, keeps the last seven copies, runs every night and logs what it did.

Open the project: Automate a backup

Project 2 · 14 min read

Health check

A script that checks disk, memory, load and services, writes a report, and exits with a status a monitor can act on.

Open the project: Health check

Project 3 · 15 min read

Bulk users from a file

Create and update user accounts from a CSV file, with validation, a dry-run mode and a summary, safe to run again.

Open the project: Bulk users from a file

Project 4 · 13 min read

Deploy script

Release a new version of a site into a timestamped directory, switch to it with a symlink, check it, and roll back on failure.

Open the project: Deploy script