AI News Daily한국어
← AI tool tips
Claude Code✓ Confirmed in official docsAdded Nov 25

How to block shell commands that run when a Claude Code skill loads with the PreSkillShellCommand hook

Shell commands written in a skill file as !`<command>` or in a block fenced with three backticks followed by ! run as soon as the skill loads, and PreToolUse hooks don't fire for them. Claude Code 2.1.332 (November 25) added a PreSkillShellCommand hook event that catches these injected commands, including the ones that run when built-in commands such as /security-review load.

Update to 2.1.332 or later first. Earlier versions run injected commands without calling the hook, and 2.1.331 opens each interactive terminal session with an Unknown hook event "PreSkillShellCommand" was ignored warning dialog if a settings file names the event.

  1. Save a blocking script as .claude/hooks/block-rm.sh and make it executable with chmod +x. This example from the docs denies any command containing rm -rf. It needs jq.
#!/bin/bash
COMMAND=$(jq -r '.tool_input.command')

if echo "$COMMAND" | grep -q 'rm -rf'; then
  jq -n '{
    hookSpecificOutput: {
      hookEventName: "PreToolUse",
      permissionDecision: "deny",
      permissionDecisionReason: "Destructive command blocked by hook"
    }
  }'
else
  exit 0
fi

Leaving hookEventName as PreToolUse lets you reuse the same script on PreToolUse.

  1. Register the hook in .claude/settings.json (macOS and Linux):
{
  "hooks": {
    "PreSkillShellCommand": [
      {
        "matcher": "Bash|PowerShell",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh", "args": [], "onFailure": "block" }
        ]
      }
    ]
  }
}

The Bash|PowerShell matcher covers both shells. Without onFailure: "block", the command runs anyway when the script fails or times out. Async hooks can't block, and prompt or agent hooks can't run on this event, so they block every command they match. Define the hook in a settings file or a plugin: hooks in a skill's own frontmatter are skipped on its first load in a session and on loads inside a subagent.

  1. Test it. Create .claude/skills/hook-test/SKILL.md with this single line, start a terminal session and type /hook-test. It's an echo, so nothing gets deleted.
!`echo 'rm -rf'`

You should see Not run in /hook-test: echo 'rm -rf' (blocked by hook: Destructive command blocked by hook).

Keep a few limits in mind. The hook can only block: returning ask, defer or any updatedInput is treated as a deny, and there is no way to approve or change a command. Claude reads [not run: blocked by hook: <reason>] in place of the blocked command's output, and the skill still loads. Returning continue: false makes the skill invocation fail. The hook only sees commands that passed the injected-command permission check, and it also runs in bypassPermissions mode. In auto mode, a command that needs approval doesn't run during loading, and if Claude runs it later as a tool call, PreToolUse fires instead, so attach the script to both events. The hook input also carries command_name (the skill), command_source and invoked_by (user, claude or other), so you can treat skills differently.

Get notified when a new tip lands

Tip alerts only go out in the app. Install it and turn notifications on.

Get the app

All tips →