$npx -y skills add jackspace/ClaudeSkillz --skill claude-code-bash-patternsComprehensive knowledge for using the Bash tool in Claude Code effectively. This skill should be used when orchestrating CLI tools, configuring hooks, setting up automation workflows, managing git operations, handling multi-command patterns, or encountering Bash tool errors. Cove
| 1 | # Claude Code Bash Patterns |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-11-07 |
| 5 | **Dependencies**: Claude Code CLI (latest version) |
| 6 | **Official Docs**: https://docs.claude.com/en/docs/claude-code/tools |
| 7 | |
| 8 | --- |
| 9 | |
| 10 | ## Quick Start (10 Minutes) |
| 11 | |
| 12 | ### 1. Understanding the Bash Tool |
| 13 | |
| 14 | The Bash tool is Claude Code's primary interface for executing command-line operations. Unlike specialized tools (Read, Grep, Glob), the Bash tool provides direct shell access for complex workflows. |
| 15 | |
| 16 | **Key Characteristics**: |
| 17 | - **Session Persistence**: Commands run in a persistent bash session within a conversation |
| 18 | - **Environment Inheritance**: Inherits environment variables and working directory |
| 19 | - **Output Limit**: Truncates output at 30,000 characters |
| 20 | - **Default Timeout**: 2 minutes (configurable up to 10 minutes) |
| 21 | |
| 22 | **When to Use Bash Tool**: |
| 23 | - ✅ Running CLI tools (git, npm, wrangler, gh, etc.) |
| 24 | - ✅ Command chaining (sequential operations) |
| 25 | - ✅ Process orchestration (build, test, deploy) |
| 26 | - ✅ Environment setup and management |
| 27 | |
| 28 | **When NOT to Use Bash Tool**: |
| 29 | - ❌ Reading files → Use **Read** tool instead |
| 30 | - ❌ Searching file patterns → Use **Glob** tool instead |
| 31 | - ❌ Searching content → Use **Grep** tool instead |
| 32 | - ❌ Editing files → Use **Edit** tool instead |
| 33 | |
| 34 | ### 2. Basic Command Patterns |
| 35 | |
| 36 | ```bash |
| 37 | # Single command |
| 38 | npm install |
| 39 | |
| 40 | # Sequential with && (stops on first failure) |
| 41 | npm install && npm run build |
| 42 | |
| 43 | # Sequential with ; (continues regardless) |
| 44 | npm install ; npm run build |
| 45 | |
| 46 | # Parallel execution (make multiple Bash tool calls) |
| 47 | # Call 1: git status |
| 48 | # Call 2: git diff |
| 49 | # Call 3: git log |
| 50 | ``` |
| 51 | |
| 52 | **Golden Rule**: Use `&&` when you care about failures, `;` when you don't, and parallel calls when operations are independent. |
| 53 | |
| 54 | ### 3. Configure Your First Hook |
| 55 | |
| 56 | Hooks let you run code before/after tool execution. Here's a simple audit logger: |
| 57 | |
| 58 | ```json |
| 59 | { |
| 60 | "hooks": { |
| 61 | "PreToolUse": [ |
| 62 | { |
| 63 | "matcher": "Bash", |
| 64 | "hooks": [ |
| 65 | { |
| 66 | "type": "command", |
| 67 | "command": "echo \"[$(date -Iseconds)] $(echo \"$CLAUDE_TOOL_INPUT\" | jq -r '.tool_input.command')\" >> ~/.claude/bash-audit.log" |
| 68 | } |
| 69 | ] |
| 70 | } |
| 71 | ] |
| 72 | } |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | Save this to `~/.claude/settings.json` to log every bash command with timestamp. |
| 77 | |
| 78 | --- |
| 79 | |
| 80 | ## The Five Core Patterns |
| 81 | |
| 82 | ### Pattern 1: Command Chaining |
| 83 | |
| 84 | **When to Use**: Sequential operations where each depends on previous success |
| 85 | |
| 86 | **Syntax**: `command1 && command2 && command3` |
| 87 | |
| 88 | **Example**: Build and deploy workflow |
| 89 | ```bash |
| 90 | npm install && npm run build && npx wrangler deploy |
| 91 | ``` |
| 92 | |
| 93 | **Why It Matters**: |
| 94 | - Stops on first failure (prevents cascading errors) |
| 95 | - Maintains clean error messages (know exactly what failed) |
| 96 | - Saves tokens (no need to check status between commands) |
| 97 | |
| 98 | **Anti-Pattern**: Using `;` when you care about failures |
| 99 | ```bash |
| 100 | # ❌ Wrong: Continues even if install fails |
| 101 | npm install ; npm run build |
| 102 | |
| 103 | # ✅ Correct: Stops if install fails |
| 104 | npm install && npm run build |
| 105 | ``` |
| 106 | |
| 107 | **Advanced**: Conditional execution with `||` |
| 108 | ```bash |
| 109 | # Run tests, or echo failure message |
| 110 | npm test || echo "Tests failed, not deploying" |
| 111 | |
| 112 | # Try npm ci, fall back to npm install |
| 113 | npm ci || npm install |
| 114 | ``` |
| 115 | |
| 116 | ### Pattern 2: Parallel Execution |
| 117 | |
| 118 | **When to Use**: Independent operations that can run simultaneously |
| 119 | |
| 120 | **How**: Make multiple Bash tool calls in a single message |
| 121 | |
| 122 | **Example**: Git workflow pre-commit analysis |
| 123 | ``` |
| 124 | # Claude makes 3 parallel Bash calls in one message: |
| 125 | Call 1: git status |
| 126 | Call 2: git diff --staged |
| 127 | Call 3: git log -5 --oneline |
| 128 | ``` |
| 129 | |
| 130 | **Benefits**: |
| 131 | - ~40% faster than sequential (no waiting between calls) |
| 132 | - Reduces context usage (all results arrive together) |
| 133 | - Better user experience (appears instant) |
| 134 | |
| 135 | **Important**: Only parallelize truly independent operations. If Call 2 depends on Call 1's output, run sequentially. |
| 136 | |
| 137 | ### Pattern 3: HEREDOC for Multi-Line Content |
| 138 | |
| 139 | **When to Use**: Git commits |