shell-error-handling

Implement trap-based cleanup and exit code handling in Bash scripts.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill shell-error-handling
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: shell-error-handling
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-shellcheck/skills/shell-error-handling
Command: npx skills add https://github.com/TheBushidoCollective/han --skill shell-error-handling

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill teaches patterns for robust error handling, traps, and cleanup in Bash scripts.

Core Features & Use Cases

  • Exit codes: Standard codes and checking.
  • Trap for cleanup: Cleanup routines with traps.
  • Defensive programming: Prerequisites and input validation.

Quick Start

Add a trap-based cleanup snippet to a shell script.

Frequently Asked Questions about shell-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle errors in Bash scripts?

Error handling in Bash involves checking exit codes, using set -e to exit on failure, and applying trap commands for cleanup. Standard patterns include validating return values, routing errors to stderr, and executing cleanup routines when scripts exit or encounter failures.

What are Bash traps and how do I use them for cleanup?

Traps are Bash handlers that execute code when the shell receives signals or exits. Use trap 'cleanup_function' EXIT to run cleanup routines—closing files, removing temporary resources, or logging—automatically when your script terminates, whether normally or due to error.

How do I validate prerequisites and inputs in shell scripts?

Defensive shell programming checks prerequisites before execution and validates inputs early. Test for required commands, verify file existence, confirm correct argument counts, and validate data formats. This catches problems immediately and prevents scripts from executing with incomplete or invalid state.

Why should I use exit codes in shell scripts?

Exit codes signal success or failure to calling processes and enable reliable error detection. Standard conventions—0 for success, non-zero for specific failure types—let scripts chain operations, conditionally branch on results, and communicate failures to automation tools and logs.

How do I create secure temporary files in Bash?

Use mktemp to generate unique temporary filenames with restricted permissions, preventing race conditions and unauthorized access. Pair it with trap cleanup to remove temporary resources reliably, even if the script fails or is interrupted.