---
title: "堅牢な Bash スクリプトを書く（エージェント用スキル） | Orchestor"
description: "本番向けのスクリプトに、防御的な Bash プログラミングの手法を適用します。耐障害性と安全性が必要なシェルスクリプト、CI/CD パイプライン、システムユーティリティの作成に使います。wshobson/agents が公開している「堅牢な Bash スクリプトを書く」の使い方を確認できます。"
canonical_url: "https://www.orchestor.io/ja/skills/wshobson/agents/bash-defensive-patterns"
markdown_url: "https://www.orchestor.io/ja/skills/wshobson/agents/bash-defensive-patterns.md"
---

[c96b5f6](https://github.com/wshobson/agents/commit/c96b5f6552dba32e4492307493ee613581430715)[原文を開く](https://github.com/wshobson/agents/blob/c96b5f6552dba32e4492307493ee613581430715/plugins/shell-scripting/skills/bash-defensive-patterns/SKILL.md)

wshobson2026/11/24

原文 (選択中)

未翻訳

プレビュー (選択中)

40 行（空行を除く 32 行） · 1.94 KB原文

```yaml
name: bash-defensive-patterns
description: Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.
```

# Bash Defensive Patterns

Comprehensive guidance for writing production-ready Bash scripts using defensive programming techniques, error handling, and safety best practices to prevent common pitfalls and ensure reliability.

## When to Use This Skill

* Writing production automation scripts
* Building CI/CD pipeline scripts
* Creating system administration utilities
* Developing error-resilient deployment automation
* Writing scripts that must handle edge cases safely
* Building maintainable shell script libraries
* Implementing comprehensive logging and monitoring
* Creating scripts that must work across different platforms

## Detailed patterns and worked examples

Detailed pattern documentation lives in details.md. Read that file when the navigation tier above is insufficient.

## Best Practices Summary

1. **Always use strict mode** \- `set -Eeuo pipefail`
2. **Quote all variables** \- `"$variable"` prevents word splitting
3. **Use \[\[\]\] conditionals** \- More robust than \[ \]
4. **Implement error trapping** \- Catch and handle errors gracefully
5. **Validate all inputs** \- Check file existence, permissions, formats
6. **Use functions for reusability** \- Prefix with meaningful names
7. **Implement structured logging** \- Include timestamps and levels
8. **Support dry-run mode** \- Allow users to preview changes
9. **Handle temporary files safely** \- Use mktemp, cleanup with trap
10. **Design for idempotency** \- Scripts should be safe to rerun
11. **Document requirements** \- List dependencies and minimum versions
12. **Test error paths** \- Ensure error handling works correctly
13. **Use `command -v`** \- Safer than `which` for checking executables
14. **Prefer printf over echo** \- More predictable across systems

## Structured data

```json
[
  [
    {
      "@context": "https://schema.org",
      "@type": "Organization",
      "name": "Orchestor",
      "url": "https://www.orchestor.io",
      "sameAs": [
        "https://orchestor.io",
        "https://www.orchestor.io",
        "https://docs.orchestor.io"
      ],
      "@id": "https://www.orchestor.io/#organization",
      "alternateName": [
        "オーケスター"
      ],
      "logo": "https://www.orchestor.io/images/common/ogp/orchestor-ogp.png",
      "parentOrganization": {
        "@type": "Organization",
        "name": "ドットメディア株式会社",
        "url": "https://www.dotmedia.co.jp"
      }
    },
    {
      "@context": "https://schema.org",
      "@type": "WebSite",
      "url": "https://www.orchestor.io",
      "name": "Orchestor",
      "description": "Orchestor で画像・動画・音声を作り、参照素材を Remix できます。",
      "@id": "https://www.orchestor.io/#website",
      "alternateName": [
        "オーケスター"
      ],
      "inLanguage": "ja",
      "publisher": {
        "@id": "https://www.orchestor.io/#organization"
      }
    }
  ],
  {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "@id": "https://www.orchestor.io/skills/wshobson/agents/bash-defensive-patterns#breadcrumb",
    "itemListElement": [
      {
        "@type": "ListItem",
        "position": 1,
        "name": "Orchestor",
        "item": "https://www.orchestor.io/"
      },
      {
        "@type": "ListItem",
        "position": 2,
        "name": "スキル",
        "item": "https://www.orchestor.io/skills"
      },
      {
        "@type": "ListItem",
        "position": 3,
        "name": "堅牢な Bash スクリプトを書く",
        "item": "https://www.orchestor.io/skills/wshobson/agents/bash-defensive-patterns"
      }
    ]
  }
]
```

---

[Documentation index](https://www.orchestor.io/llms.txt)
