Skip to main content
DevOpsLabTH.dev

Bash

Move from typing commands to writing scripts that survive Monday morning. Loops, exit codes, traps, defensive scripting.

beginner16 labs
Recommended first

You can still start this course now. Earlier courses give you the mental model the labs here assume.

By the end you'll be able to
  • Write scripts that fail loudly instead of silently
  • Handle arguments, exit codes, and traps like a grown-up
  • Loop over real data without choking on whitespace
  • Debug a broken script in production without rewriting it

Labs

  1. 01
    Make a script runnable
    A script is a text file with a shebang on line one and the execute bit set. Three small steps to turn typed commands into a program.
    15 min
    Start
  2. 02
    Read what every command returns
    Every command returns a number when it finishes. Zero means success, anything else means failure. This number is how scripts detect errors.
    15 min
    Start
  3. 03
    Pass arguments to scripts
    $1 is the first argument. $@ is all of them. $# is how many. Three variables that turn a script from fixed to reusable.
    15 min
    Start
  4. 04
    Local variables vs exported variables
    A plain assignment lives in the current shell. export marks the variable so child processes inherit it. The same name behaves differently depending on which side of export you set it.
    15 min
    Start
  5. 05
    Parameter expansion for safe scripts
    Bash parameter expansion does more than `$VAR`. Default values, stripping a suffix, and stripping a prefix all come from the same `${...}` syntax.
    20 min
    Start
  6. 06
    Branch on conditions with if
    Bash `if [ ... ]; then ... elif ... else ... fi` is the workhorse of every shell script. Write a script that decides between two answers based on a numeric argument.
    15 min
    Start
  7. 07
    Iterate with the three for-loop forms
    A for loop walks a list of values one at a time. The list can be words you type, files a glob expands to, or the output of another command.
    20 min
    Start
  8. 08
    Read a file line by line with while
    A for loop splits on whitespace and breaks on lines that contain spaces. while read pulls one full line at a time, which is the right shape for log files, CSVs, and any list with one item per line.
    15 min
    Start
  9. 09
    Branch on patterns with case
    A long if/elif chain that compares one variable against many values is what case was built for. Cleaner syntax, faster to read, and a wildcard arm for the unmatched fallthrough.
    15 min
    Start
  10. 10
    Define functions and scope variables
    Functions group commands behind a name. local keeps variables inside the function so they do not leak into the caller.
    15 min
    Start
  11. 11
    Reuse code with source
    source loads another script into the current shell. The variables and functions it declares become yours, without spawning a child process.
    15 min
    Start
  12. 12
    Defensive scripting with set -euo pipefail
    A bare Bash script keeps going after errors and treats unset variables as empty strings. set -euo pipefail flips those defaults so problems surface early.
    20 min
    Start
  13. 13
    Guarantee cleanup with trap EXIT
    Use trap to remove temporary files even when a script errors out or exits early.
    20 min
    Start
  14. 14
    Parse JSON with jq
    jq is the standard tool for reading JSON in a shell pipeline. Extract fields, index arrays, strip quotes.
    15 min
    Start
  15. 15
    Parse command-line flags with getopts
    Use the built-in `getopts` loop to read short flags in a script. Handle a value-bearing flag and a plain switch.
    25 min
    Start
  16. 16
    Capstone, a deploy gate script
    An open-ended problem with no prescribed solution. Write a script that reads a service status file, reports how many services are down, and exits non-zero when any are, so a deploy pipeline can gate on it. Any approach that behaves correctly passes.
    25 min
    Start