shell-error-handling

Implement error handling, cleanup, and debugging patterns in shell scripts.

1|Updated Jun 18, 2025
One-click install
npx skills add https://github.com/knopki/dotfiles --skill shell-error-handling-knopki
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: shell-error-handling
Source: https://github.com/knopki/dotfiles/tree/main/home/private_dot_config/opencode/skills/shell-error-handling
Command: npx skills add https://github.com/knopki/dotfiles --skill shell-error-handling-knopki

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides patterns and best practices for implementing robust error handling, cleanup routines, and debugging in shell scripts, ensuring reliability and maintainability.

Core Features & Use Cases

  • Exit Code Management: Understand and utilize standard exit codes for clear success/failure indication.
  • Error Checking: Implement patterns to reliably check command success and handle failures gracefully.
  • Cleanup with Traps: Ensure resources are cleaned up automatically upon script exit, regardless of how it terminates (success, error, or interrupt).
  • Error Reporting: Standardize error messages to stderr and implement functions for logging and fatal errors.
  • Defensive Programming: Validate prerequisites, inputs, and create secure temporary files/directories.
  • Debugging: Enable debug modes and trace execution for easier troubleshooting.
  • Error Recovery: Implement retry and fallback mechanisms for resilient script execution.
  • Use Case: When writing a complex deployment script, use this Skill's patterns to ensure temporary files are removed, services are stopped cleanly, and meaningful error messages are displayed if any step fails.

Quick Start

Use the shell-error-handling skill to implement a trap for cleaning up temporary files upon script exit.

Frequently Asked Questions about shell-error-handling

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

FAQPage Schema
How do I clean up temporary files automatically when a bash script fails or gets interrupted?

To clean up temporary files automatically, implement shell trap mechanisms to capture exit signals and execute cleanup routines upon script termination. Traps ensure resources are removed regardless of whether the script exits successfully, fails, or receives an interrupt signal.

What is the best way to handle errors and exit codes in shell scripts?

The best way to handle errors in shell scripts is to implement standard exit code management and reliable error checking patterns. This approach ensures clear success or failure indication and allows scripts to handle command failures gracefully instead of continuing execution.

How do I validate prerequisites and user input for secure shell scripting?

Validate prerequisites and user input in shell scripting by applying defensive programming checks before execution. This includes verifying required commands exist, validating input data formats, and creating secure temporary files and directories to prevent unsafe script execution.

How does error reporting via stderr work in bash scripting?

Error reporting via stderr in bash scripting works by directing error messages to the standard error stream, separating them from standard output. Standardizing error messages to stderr and using dedicated logging functions ensures diagnostic information is cleanly parsed and reviewed.

Can I implement retry and fallback mechanisms for resilient shell script execution?

You can implement retry and fallback mechanisms to achieve resilient shell script execution. Error recovery patterns allow scripts to automatically retry failed commands or switch to alternative fallback operations, ensuring reliable execution despite transient network or system failures.

How do I debug a shell script using set -x and trace execution?

Debug a shell script by enabling debug modes and tracing execution using techniques like set -x. This method prints executed commands and their arguments to stderr, providing step-by-step visibility into script logic for easier troubleshooting and resolving unexpected behavior.