secondsky/claude-skills / plugins/claude-code-bash-patterns/skills/claude-code-bash-patterns
claude-code-bash-patterns
FlaggedClaude 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.4 KB |
| references/cli-tool-integration.md | markdown | 4.8 KB |
| references/git-workflows.md | markdown | 9.7 KB |
| references/hooks-examples.md | markdown | 6.7 KB |
| references/security-best-practices.md | markdown | 4.1 KB |
| references/troubleshooting-guide.md | markdown | 8.4 KB |
| scripts/bash-audit-logger.shexecutable | script | 4.7 KB |
| scripts/dangerous-command-guard.pyexecutable | script | 6.4 KB |
| scripts/package-manager-enforcer.shexecutable | script | 2.3 KB |
| templates/.envrc.example | other | 5.7 KB |
| templates/custom-command-template.md | markdown | 5.1 KB |
| templates/dangerous-commands.json | config | 2.9 KB |
| templates/github-workflow.yml | config | 3.1 KB |
| templates/settings.json | config | 3.1 KB |
scripts/bash-audit-logger.sh
View on GitHub#!/bin/bash
#
# Bash Audit Logger - PreToolUse Hook for Claude Code
#
# Logs all bash commands with timestamps for compliance and debugging.
#
# Usage:
# Configure as PreToolUse hook in ~/.claude/settings.json:
#
# {
# "hooks": {
# "PreToolUse": [
# {
# "matcher": "Bash",
# "hooks": [
# {
# "type": "command",
# "command": "~/.claude/hooks/bash-audit-logger.sh"
# }
# ]
# }
# ]
# }
# }
#
# Input:
# Reads JSON from stdin (Claude Code PreToolUse payload shape):
# {"tool_input": {"command": "...", "description": "..."}, ...}
# Previously this hook read $CLAUDE_TOOL_INPUT (env var) — that was wrong;
# Claude Code sends the payload on stdin, not via env. Fixed.
#
# Secret handling:
# Known secret shapes (Bearer tokens, Authorization headers, password=,
# --token, OpenAI/Slack/AWS/GitHub token prefixes) are REDACTED before
# the command is written to the log. This is best-effort; users with
# custom secret formats should extend the redact_secrets() function below.
#
# Output:
# Appends to ~/.claude/bash-audit.log with 0600 permissions.
#
# Exit Codes:
# 0 = Always (this hook never blocks)
# Restrictive umask for any file we create (log, lock).
umask 077
# Configuration
LOG_FILE="${HOME}/.claude/bash-audit.log"
MAX_LOG_SIZE=10485760 # 10MB
# ---------------------------------------------------------------------------
# redact_secrets: scrub common secret shapes from a string before logging.
# Best-effort; extend this function for project-specific secret formats.
# Uses `sed -E` (extended regex) — supported by both GNU and BSD sed.
# ---------------------------------------------------------------------------
redact_secrets() {
local s="$1"
s=$(printf '%s' "$s" | sed -E \
-e 's/(Bearer[[:space:]]+)[A-Za-z0-9._-]+/\1***REDACTED***/gI' \
-e 's/(Authorization:[[:space:]]*Bearer[[:space:]]+)[A-Za-z0-9._-]+/\1***REDACTED***/gI' \
-e 's/(password=)[^[:space:]]+/\1***REDACTED***/gI' \
-e 's/(PGPASSWORD=)[^[:space:]]+/\1***REDACTED***/gI' \
-e 's/(--token[[:space:]]+)[^[:space:]]+/\1***REDACTED***/gI' \
-e 's/(sk-[a-zA-Z0-9]{20,})/***REDACTED-sk_***/g' \
-e 's/(ghp_[A-Za-z0-9]{36})/***REDACTED-ghp_***/g' \
-e 's/(AKIA[0-9A-Z]{16})/***REDACTED-AKIA_***/g' \
-e 's/(xox[baprs]-[A-Za-z0-9-]+)/***REDACTED-xox_***/g')
printf '%s' "$s"
}
# Create log directory if doesn't exist
mkdir -p "$(dirname "$LOG_FILE")"
# ---------------------------------------------------------------------------
# Parse command from stdin (Claude Code PreToolUse JSON payload).
# ---------------------------------------------------------------------------
INPUT=$(cat)
COMMAND=$(printf '%s' "$INPUT" | jq -r '.tool_input.command // "N/A"')
DESCRIPTION=$(printf '%s' "$INPUT" | jq -r '.tool_input.description // "No description"')
# Redact secrets before any write
COMMAND=$(redact_secrets "$COMMAND")
DESCRIPTION=$(redact_secrets "$DESCRIPTION")
# Get timestamp
TIMESTAMP=$(date -Iseconds 2>/dev/null || date '+%Y-%m-%dT%H:%M:%S%z')
# Log entry format: [timestamp] user@host:pwd $ command
LOG_ENTRY="[${TIMESTAMP}] ${USER}@$(hostname):$(pwd) $ ${COMMAND}"
# ---------------------------------------------------------------------------
# Append to log file with rotation. Rotation (mv + recreate) has an inherent
# race between concurrent writers; wrap the size-check + rotate + append in a
# flock if available. flock is Linux-only by default; on macOS it must be
# installed (e.g. `brew install util-linux`). If unavailable we fall back to
# the non-atomic append and surface a TODO.
# ---------------------------------------------------------------------------
append_with_rotation() {
# On first creation, ensure 0600 perms.
if [ ! -f "$LOG_FILE" ]; then
: > "$LOG_FILE"
chmod 600 "$LOG_FILE" 2>/dev/null || true
fi
if [ -n "${HAS_FLOCK:-}" ]; then
(
flock -x 200 || exit 1
_rotate_and_append
) 200>"${LOG_FILE}.lock"
else
# TODO: install flock (brew install util-linux) for atomic rotation.
_rotate_and_append
fi
}
_rotate_and_append() {
local log_size
log_size=$(stat -f%z "$LOG_FILE" 2>/dev/null || stat -c%s "$LOG_FILE" 2>/dev/null || echo 0)
if [ "$log_size" -gt "$MAX_LOG_SIZE" ]; then
mv "$LOG_FILE" "${LOG_FILE}.old"
echo "[${TIMESTAMP}] Log rotated (size: ${log_size} bytes)" > "$LOG_FILE"
chmod 600 "$LOG_FILE" 2>/dev/null || true
fi
echo "$LOG_ENTRY" >> "$LOG_FILE"
}
# Detect flock once
if command -v flock >/dev/null 2>&1; then
HAS_FLOCK=1
fi
append_with_rotation
# Always allow execution
exit 0
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 466k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 466k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 466k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |