shell-scripting

Apply error handling, argument parsing, portability, and security best practices to Bash and Zsh scripts.

4|Updated Feb 16, 2017
One-click install
npx skills add https://github.com/nekorush14/dotfiles --skill shell-scripting-nekorush14
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: shell-scripting
Source: https://github.com/nekorush14/dotfiles/tree/main/configs/claude/skills/shell-scripting
Command: npx skills add https://github.com/nekorush14/dotfiles --skill shell-scripting-nekorush14

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides best practices for Bash and Zsh scripting, focusing on error handling, argument parsing, portability, and security to improve script reliability and maintainability.

Core Features & Use Cases

  • Fail-Fast & Defensive Programming: Establishes robust error handling and input validation.
  • Portability & POSIX Compliance: Promotes portable scripts across environments.
  • Argument Parsing Patterns: Demonstrates robust CLI option handling.
  • Security Best Practices: Guides safe file/variable handling and mitigates common risks.

Quick Start

Create a script that uses set -euo pipefail, includes argument parsing for --output, and validates inputs before performing file operations.

Frequently Asked Questions about shell-scripting

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

FAQPage Schema
How do I write bash scripts that fail safely and handle errors properly?

Error handling in bash requires `set -euo pipefail` at the script start to enable fail-fast behavior: exit on errors, treat undefined variables as fatal, and propagate pipe failures. Combine this with explicit error paths, input validation, and defensive checks before file operations to catch issues early and prevent silent failures.

What are POSIX shell scripting best practices for portability across different systems?

POSIX compliance ensures scripts run across bash, zsh, and other POSIX shells on any Unix-like system. Avoid shell-specific syntax, use portable argument parsing patterns, prefer POSIX builtins over GNU extensions, and document language-specific features. This maximizes compatibility without rewriting for each environment.

How do I securely handle files and variables in shell scripts to prevent injection attacks?

Secure shell scripting requires defensive input validation, quoting all variable expansions, avoiding eval, using explicit file permissions, and sanitizing command arguments. Apply secure defaults like umask settings, validate user input before processing, and use explicit error paths to prevent unintended command execution or file access.

What's the best way to parse command-line arguments in a bash or zsh script?

Robust argument parsing uses getopt or getopts patterns with explicit handling for flags, options with values, and positional arguments. Validate all inputs before use, provide clear error messages for invalid options, and document expected arguments. This makes scripts maintainable and prevents silent argument misinterpretation.

Why should I use shell scripts instead of other languages for automation tasks?

Shell scripts excel at system automation, file operations, and Unix tool orchestration without external dependencies. Use them for quick automation, deployment tasks, and tools that combine existing commands. However, for complex logic, data processing, or cross-platform GUIs, compiled or interpreted languages often provide better maintainability and performance.

Can I debug shell scripts to find where errors occur and improve reliability?

Debug shell scripts using `set -x` for command tracing, adding debug output before risky operations, and testing argument parsing separately. Combine defensive input validation with explicit error messages that identify failure points. Use shellcheck linting to catch common mistakes before runtime.