Claude Code hooks कैसे लगाएँ — हिंदी गाइडClaude Code hooks — Hindi guide
हर Bash command से पहले आपका script; exit 2 = रोकYour script runs before every Bash command; exit 2 = block
क्या चाहिएWhat you needClaude Code (Claude Code शुरू करना — install, /init, CLAUDE.md, permissions, plan mode), jq (Mac: brew install jq)Claude Code (Start with Claude Code — install, /init, CLAUDE.md, permissions, plan mode), jq (Mac: brew install jq)
7 आसान कदम7 simple steps
- Hook = आपका छोटा script जो Claude के किसी tool से पहले या बाद अपने आप चलता है।A hook is your small script that runs automatically before or after one of Claude's tools.
- PreToolUse hook Bash command चलने से पहले चलता है। Command JSON में stdin पर आती है (tool_input.command)।A PreToolUse hook runs before a Bash command. The command arrives as JSON on stdin (tool_input.command).
- Script exit 2 करे तो command रुक जाती है और stderr का संदेश Claude को दिखता है। exit 0 = सामान्य रास्ता (permission वाला सवाल वैसे ही आएगा)।If the script exits 2 the command is blocked and its stderr is shown to Claude. Exit 0 = normal flow (the usual permission prompt still applies).
- नीचे वाला block project folder में Terminal पर paste कीजिए — ये script और .claude/settings.local.json बनाता है — आपकी settings.json और उसके permissions को नहीं छूता। पहले से settings.local.json हो तो सिर्फ़ "hooks" वाला हिस्सा उसमें जोड़िए।Paste the block below into Terminal in your project folder — it creates the script and .claude/settings.local.json, leaving your settings.json and its permissions untouched. If you already have a settings.local.json, add only the "hooks" part.
- Claude Code खोलकर /hooks लिखिए — PreToolUse → Bash दिखना चाहिए।Open Claude Code and type /hooks — you should see PreToolUse → Bash.
- Test: "test-delete folder बनाकर rm -rf से मिटाने की कोशिश करो" — "Blocked" आना चाहिए।Test: "make a test-delete folder and try to delete it with rm -rf" — you should see "Blocked".
- Hook आपके computer पर आपकी अनुमति से चलता है। किसी और के repo में hooks हों तो पढ़कर ही folder को trust कीजिए।Hooks run on your computer with your permissions. If someone else's repo ships hooks, read them before trusting the folder.
Copy करने वाला promptPrompt to copy
[ ] वाली जगह अपनी बात भरिए। Claude या Gemini — दोनों में चलता है।Fill in the [ ] parts. Works in Claude or Gemini.
mkdir -p .claude/hooks && cat > .claude/hooks/block-danger.sh <<'EOF'
#!/bin/bash
input=$(cat)
command=$(jq -r '.tool_input.command // empty' <<<"$input")
if echo "$command" | grep -Eq '(^|[;&| ])rm +-[a-zA-Z]*[rf]|(^|[;&| ])sudo |git push .*(--force|-f( |$))'; then
echo "Blocked: ख़तरनाक command ($command) — user से पूछो, ख़ुद मत चलाओ" >&2
exit 2
fi
exit 0
EOF
chmod +x .claude/hooks/block-danger.sh
# settings.local.json में — पुरानी settings.json के permissions नहीं मिटेंगे
cat > .claude/settings.local.json <<'EOF'
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-danger.sh" }
]
}
]
}
}
EOFmkdir -p .claude/hooks && cat > .claude/hooks/block-danger.sh <<'EOF'
#!/bin/bash
input=$(cat)
command=$(jq -r '.tool_input.command // empty' <<<"$input")
if echo "$command" | grep -Eq '(^|[;&| ])rm +-[a-zA-Z]*[rf]|(^|[;&| ])sudo |git push .*(--force|-f( |$))'; then
echo "Blocked: ख़तरनाक command ($command) — user से पूछो, ख़ुद मत चलाओ" >&2
exit 2
fi
exit 0
EOF
chmod +x .claude/hooks/block-danger.sh
# settings.local.json में — पुरानी settings.json के permissions नहीं मिटेंगे
cat > .claude/settings.local.json <<'EOF'
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-danger.sh" }
]
}
]
}
}
EOF
Claude में खोलेंOpen in Claude
पाठशाला में ✓ लगाकर सीखिएLearn it in the AI School with ✓ ticks