Building tools in Bash: testing, libraries and distribution
You turn Bash scripts into tools teams trust: library structure, testing with bats, configuration, logging, versioning and CI checks.
Level 401 · 18 lessons · 15 practice labs
Lessons
Structure
- When Bash is the right tool, and when it isn't
- Tool project layout: bin, lib, etc and tests
- Library files and safe sourcing
- Precedence of config files, environment and options
- Short task: split an 800-line script into modules
Testing
- First tests with bats
- Mocking external commands: fake tools via PATH
- Testing the error paths
- Short task: write tests for the critical functions and fix what you find
Operations
- Structured logging: time, level and JSON lines
- Idempotency: a script that gives the same result every time
- Dry runs, confirmation and undo
- Secrets in scripts: environment, file permissions and leaks
- Short task: run a setup script ten times without breaking the system
Distribution
- Versioning, changelogs and --version
- Writing shell completion and a man page
- ShellCheck, shfmt and bats in CI
- Capstone: release the team's server-handover tool as a tested version
Practice labs
- Split a long script into modules: Split a long single-file script into a bin, lib and etc layout with behaviour unchanged.
- Source a library wherever the tool is called from: Fix a tool that cannot find its library when run from another directory or via a symlink so it loads relative to itself.
- Implement configuration precedence: Make the tool read its settings in the order default, system file, user file, environment variable and option.
- Write a first bats test for a function: Write a bats test covering three cases of a version comparison function and show it passes.
- Test with a fake stand-in for an external command: Test a function that calls systemctl using a fake command placed on PATH, without touching real services.
- Test the error paths and fix what you find: Write tests for missing file, forbidden directory and bad input cases and fix the real bug they expose.
- Make a setup script safe to run ten times: Fix a setup script that appends lines again on every run so it gives the same result however often it runs.
- Add confirmation and undo to a tool: Add a confirmation prompt, a dry run and a subcommand that undoes the last change to a config-changing tool.
- Stop a script leaking a password: Fix a script that exposes a password in its command-line arguments and debug output so it reads from a permission-restricted file.
- Add structured logging to a tool: Add a logging function to the tool that writes JSON lines with time, level and message fields.
- Write a man page for a tool: Write a man page covering the tool's usage, options and exit codes and confirm it opens with man.
- Write shell completion for a tool: Write and install a bash completion script that completes the tool's subcommands, its options and the host names from the inventory file.
- Add --version and a changelog to a tool: Add a single-sourced version number, a --version option and a formatted changelog to the tool.
- Combine ShellCheck, shfmt and bats in one check: Write a check script that runs the three tools in order and stops if any fails, then fix the errors in the repository.
- Release the server-handover tool as a tested version: Package the team's handover tool as a versioned archive with passing tests and an install script.
About this course
You turn Bash scripts into tools teams trust: library structure, testing with bats, configuration, logging, versioning and CI checks.
What you will learn
- Structure
- Testing
- Operations
- Distribution
Prerequisites
You should have practised the topics of the same path up to level 301.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 401; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.