writing-shell-scripts

Write and validate portable shell scripts with error handling and shellcheck.

1|Updated Feb 12, 2026
One-click install
npx skills add https://github.com/Chogos/claude-skills --skill writing-shell-scripts
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: writing-shell-scripts
Source: https://github.com/Chogos/claude-skills/tree/main/writing-shell-scripts
Command: npx skills add https://github.com/Chogos/claude-skills --skill writing-shell-scripts

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps you write, review, and debug shell scripts that are robust, portable, and follow best practices, preventing common errors and improving maintainability.

Core Features & Use Cases

  • Best Practices: Learn conventions for shebang, error handling (set -Eeuo pipefail), variable quoting, and function design.
  • Portability: Understand differences between GNU and BSD commands and how to write cross-platform scripts.
  • Validation: Integrate shellcheck and bash -n into your workflow for automated quality checks.
  • Use Case: You need to write a new deployment script for your application. Use this Skill to ensure it includes proper error handling, argument parsing, and cleanup, making it reliable and easy to maintain.

Quick Start

Use the writing-shell-scripts skill to generate a template for a new bash script that includes error handling and argument parsing.

Frequently Asked Questions about writing-shell-scripts

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

FAQPage Schema
How do I write a robust bash script with proper error handling?

Robust bash scripts require using `set -Eeuo pipefail` to catch errors early, properly quoting variables to prevent word splitting, and defining functions with clear structures. Using `trap` ensures cleanup actions execute if a script fails or exits unexpectedly.

What is the best way to make shell scripts portable across GNU and BSD?

Portable shell scripts require understanding functional differences between GNU and BSD utilities. You should write cross-platform scripts by identifying command-specific flags and behaviors, avoiding GNU-only extensions, and testing execution environments to ensure compatibility across different Unix-like systems.

How do I validate a shell script for syntax and best practices?

Validating shell scripts involves running `bash -n` to check for syntax errors without executing the code, and integrating `shellcheck` into your workflow. This automated validation identifies common scripting errors, improves maintainability, and enforces best practices.

Why does my bash script fail when iterating over files with spaces?

Bash scripts fail on spaced file paths due to improper variable quoting and unsafe iteration methods. You must quote variables consistently and use safe file iteration patterns, often combined with `set -u` to catch unset variables that cause unexpected word splitting.

Can I use trap for cleanup in a deployment shell script?

Yes, you can use `trap` to execute cleanup commands when your deployment script exits or receives a signal. This ensures temporary files are removed and resources are released, making the script reliable and easy to maintain even if an error occurs.