claude-code-bash-patterns
Claude Code Bash tool patterns with hooks, automation, git workflows. Use for PreToolUse hooks, command chaining, CLI orchestration, custom commands, or encountering bash permissions, command failures, security guards, hook configurations.
What this skill does
# Claude Code Bash Patterns
**Status**: Production Ready ✅ | **Last Verified**: 2025-11-18
---
## Quick Start
### Basic Command
```bash
ls -la
```
### Command Chaining
```bash
bun install && bun run build && bun test
```
### Hooks
Create `.claude-hook-pretooluse.sh`:
```bash
#!/usr/bin/env bash
# PreToolUse hook - runs before every Bash command
echo "Running: $1"
```
**Load `references/hooks-examples.md` for complete hook patterns.**
---
## The Five Core Patterns
### 1. Sequential Operations (&&)
**Use when**: Each command depends on previous success
```bash
git add . && git commit -m "message" && git push
```
**Why**: Stops chain if any command fails
---
### 2. Parallel Operations (Multiple tool calls)
**Use when**: Commands are independent
```
Message with multiple Bash tool calls in parallel
```
**Load `references/cli-tool-integration.md` for parallel patterns.**
---
### 3. Session Persistence
**Use when**: Need to maintain state across commands
```bash
# Set environment variable
export API_KEY="sk-..."
# Use in later commands (same session)
curl -H "Authorization: Bearer $API_KEY" api.example.com
```
---
### 4. Background Processes
**Use when**: Long-running tasks
```bash
npm run dev &
# Get PID with $!
```
---
### 5. Hooks for Automation
**Use when**: Need pre/post command logic
**Load `references/hooks-examples.md` for all hook types.**
---
## Critical Rules
### Always Do ✅
1. **Use && for sequential dependencies** (not semicolons)
2. **Quote paths with spaces** (`cd "path with spaces"`)
3. **Check environment before destructive ops** (rm, git push --force)
4. **Use specialized tools first** (Read, Grep, Glob before Bash)
5. **Set timeouts for long operations** (up to 10 minutes)
6. **Validate inputs** before passing to shell commands
7. **Use hooks for repeated patterns** (logging, validation)
8. **Maintain session state** (export variables once)
9. **Handle errors explicitly** (check exit codes)
10. **Document custom commands** in .claude/commands/
### Never Do ❌
1. **Never use ; for dependent commands** (use &&)
2. **Never skip quoting paths** with spaces
3. **Never run rm -rf without confirmation**
4. **Never expose secrets** in command output
5. **Never ignore timeout limits** (max 10 min)
6. **Never use bash for file operations** when specialized tools exist
7. **Never chain with newlines** (use && or ; explicitly)
8. **Never force-push to main** without explicit user request
9. **Never skip hooks** (--no-verify) without user request
10. **Never use interactive commands** (git rebase -i, git add -i)
---
## Git Workflows
### Basic Commit
```bash
git add . && git commit -m "feat: add feature"
```
### Commit with Testing
```bash
npm test && git add . && git commit -m "fix: bug fix" && git push
```
### Pull Request
```bash
git checkout -b feature/new && git add . && git commit -m "feat: new feature" && git push -u origin feature/new
```
**Load `references/git-workflows.md` for complete workflows including:**
- Feature branch workflow
- PR creation automation
- Commit message conventions
- Pre-commit validation
---
## Hooks: Advanced Automation
### PreToolUse Hook
**`.claude-hook-pretooluse.sh`**:
```bash
#!/usr/bin/env bash
COMMAND="$1"
# Log all commands
echo "[$(date)] Running: $COMMAND" >> ~/claude-commands.log
# Block dangerous patterns
if [[ "$COMMAND" =~ rm\ -rf\ / ]]; then
echo "❌ Blocked dangerous command"
exit 1
fi
```
### Hook Types
- **pretooluse** - Before every Bash command
- **stop** - Before conversation ends
- **user-prompt-submit** - After user submits message
**Load `references/hooks-examples.md` for all hook types and examples.**
---
## CLI Tool Integration
### npm/bun
```bash
bun install && bun run build
```
### wrangler (Cloudflare)
```bash
bunx wrangler deploy
```
### gh (GitHub CLI)
```bash
gh pr create --title "Fix bug" --body "Description"
```
**Load `references/cli-tool-integration.md` for complete tool patterns.**
---
## Custom Commands
Create `.claude/commands/deploy.md`:
```markdown
---
description: Deploy to production
---
Run these steps:
1. Run tests: `npm test`
2. Build: `npm run build`
3. Deploy: `wrangler deploy`
```
User can invoke with: `/deploy`
**Load `templates/custom-command-template.md` for template.**
---
## Security
### Allowlisting Tools
**`settings.json`**:
```json
{
"dangerousCommandsAllowList": [
"git push --force"
]
}
```
### Secrets Management
```bash
# ✅ Good: Use environment variables
export API_KEY="$SECURE_VALUE"
# ❌ Bad: Hardcode secrets
curl -H "Authorization: Bearer sk-abc123..."
```
**Load `references/security-best-practices.md` for complete security guide.**
---
## Common Use Cases
### Use Case 1: Test Before Commit
```bash
npm test && git add . && git commit -m "message"
```
### Use Case 2: Deploy with Validation
```bash
npm run lint && npm test && npm run build && bunx wrangler deploy
```
### Use Case 3: Multi-Repo Operations
```bash
cd repo1 && git pull && cd ../repo2 && git pull
```
### Use Case 4: Background Process
```bash
npm run dev &
```
**Load `references/cli-tool-integration.md` for more patterns.**
---
## Troubleshooting
### Issue: Command times out
**Solution**: Increase timeout or use background mode
```bash
# Background mode
npm run dev &
```
### Issue: Path with spaces fails
**Solution**: Quote the path
```bash
cd "path with spaces/file.txt"
```
### Issue: Hook blocks command
**Solution**: Check hook logic in `.claude-hook-pretooluse.sh`
**Load `references/troubleshooting-guide.md` for all issues.**
---
## When to Load References
### Load `references/git-workflows.md` when:
- Setting up git automation
- Creating PRs programmatically
- Need commit message conventions
- Want pre-commit validation patterns
### Load `references/hooks-examples.md` when:
- Creating custom hooks
- Need hook templates
- Want validation patterns
- Implementing logging/security
### Load `references/cli-tool-integration.md` when:
- Orchestrating multiple CLI tools
- Need tool-specific patterns
- Want parallel execution examples
- Troubleshooting tool integration
### Load `references/security-best-practices.md` when:
- Configuring security guards
- Setting up allowlisting
- Managing secrets
- Preventing dangerous operations
### Load `references/troubleshooting-guide.md` when:
- Debugging command failures
- Encountering timeouts
- Hooks behaving unexpectedly
- Session state issues
---
## Using Bundled Resources
### References (references/)
- **git-workflows.md** - Complete git automation patterns
- **hooks-examples.md** - All hook types with examples
- **cli-tool-integration.md** - Tool orchestration patterns
- **security-best-practices.md** - Security configuration guide
- **troubleshooting-guide.md** - Common issues and solutions
### Templates (templates/)
- **custom-command-template.md** - Custom command template
- **settings.json** - Security settings example
- **.envrc.example** - Environment variables example
- **github-workflow.yml** - GitHub Actions integration
- **dangerous-commands.json** - Dangerous patterns list
---
## Examples
### Feature Development Workflow
```bash
git checkout -b feature/oauth && \
npm test && \
git add . && \
git commit -m "feat(auth): add OAuth support" && \
git push -u origin feature/oauth
```
### CI/CD Pipeline
```bash
npm run lint && \
npm test && \
npm run build && \
bunx wrangler deploy
```
### Multi-Project Update
```bash
cd project1 && bun install && cd ../project2 && bun install
```
---
## Official Documentation
- **Claude Code Bash Tool**: https://docs.claude.com/en/docs/claude-code/tools
- **Hooks**: https://docs.claude.com/en/docs/claude-code/hooks
- **Custom Commands**: https://docs.claude.com/en/docs/claude-code/custom-commands
---
**Questions? Issues?**
1. Check `references/troubleshooting-guide.md` for common issues
2. Review `references/git-workflows.md` for git patterns
3. See `refeRelated in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.