shell-best-practices

Guide Bash script creation with strict mode and error handling best practices.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Shell scripts often break on edge cases; this Guide consolidates best practices for portability, error handling, and security.

Core Features & Use Cases

  • Shell strict mode and error handling patterns
  • Safe quoting, inputs validation, and parameter handling
  • Debugging and robust script structure

Quick Start

Create a portable script header with set -euo pipefail and a usage example.

Frequently Asked Questions about shell-best-practices

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

FAQPage Schema
How do I write bash scripts that don't break on edge cases?

Bash scripts fail on unquoted variables, unset parameters, and uncaught errors. Use strict mode—add `set -euo pipefail` after the shebang—to exit on errors, treat unset variables as fatal, and catch pipeline failures. Quote all variables and validate inputs before use.

What's the best way to handle errors in shell scripts?

Error handling in shell scripts requires three layers: strict mode flags (`set -euo pipefail`), explicit error checks after each command, and trap handlers to clean up on exit. Combine these with meaningful error messages so failures are debuggable across environments.

How do I make my bash scripts portable across different systems?

Portable shell scripts use a POSIX-compatible shebang, avoid bash-only syntax, apply consistent quoting and variable naming, and validate inputs at runtime. Use `set -euo pipefail` and test across target environments to catch environment-specific breakage early.

What security risks should I watch for when writing shell scripts?

Shell scripts are vulnerable to command injection through unquoted variables, unsafe input parsing, and inadequate parameter validation. Enforce input validation, quote all expansions, use arrays instead of string splitting, and apply strict mode to catch uninitialized variables before they execute.

Do I need a specific shebang for my bash script to run correctly?

Yes. The shebang—the first line starting with `#!`—tells the system which interpreter to use. For portable bash scripts, use `#!/bin/bash` or `#!/usr/bin/env bash`. Follow the shebang immediately with `set -euo pipefail` to enforce strict error handling.

How do I safely handle loops and arrays in shell scripts?

Unsafe loops and arrays cause word-splitting and globbing bugs. Use proper array syntax with quoted expansions like `"${array[@]}"`, iterate with `for item in "${array[@]}"; do`, and avoid bare variable expansion in loops to maintain robust, maintainable scripts.