OmniscioGuidesPreSkillShellCommand hook
Claude Code PreSkillShellCommand hook
A skill can run shell commands while it loads, through !`command` or a ```! block. This event fires once for each of those commands, so it is where a per-command block or audit goes.
Last checked against Anthropic's documentation on November 27, 2026. By the Omniscio team. Updated .
What the event fires on
The event runs before an injected command: a shell command that runs while a skill or a command such as /security-review loads. In a skill's file, those commands appear as !`<command>` or in a ```! block.
Anthropic is explicit that PreToolUse hooks do not fire for them, and asks for a hook on this event instead, via a command, HTTP or MCP tool hook that is not async. The event needs Claude Code v2.1.332 or later, in the terminal, the desktop app and the VS Code extension alike.
On 2.1.331 and older, a settings file that names this event opens a dialog at start. Update before you add the hook; an older version runs the injected commands without calling it.
Why PreToolUse misses these commands
Injected commands are not tool calls. They run before the skill's text reaches Claude, they never prompt for permission, and Claude Code checks each one against your permission rules itself. A deny permission rule aborts the invocation, with Shell command permission check failed for pattern, and outside auto mode so does any check that returns something other than allow. That is a settings rule, not a hook's answer: a hook's deny stops one command and the skill still loads. A command that passes the rule check then runs.
So a PreToolUse hook has nothing to match. This event runs after that permission check, in bypassPermissions mode too, and it is where a script that already runs on PreToolUse belongs if you want the same rule to cover a skill's own commands.
One case needs both. In auto mode, a command that would need approval does not run while the skill loads; Claude can run it afterward as a normal tool call, which fires PreToolUse and not this event.
How to add the hook
Put it in a settings file, such as a project's .claude/settings.json, your ~/.claude/settings.json, or managed settings, or ship it in a plugin. On macOS and Linux this entry runs the script in exec form, before every injected command, whichever shell runs it. Save the script at the path the entry names, and make it executable with chmod +x first:
{
"hooks": {
"PreSkillShellCommand": [
{
"matcher": "Bash|PowerShell",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-destructive.sh",
"args": [],
"onFailure": "block"
}
]
}
]
}
}The path matters with the file you put it in. ${CLAUDE_PROJECT_DIR} means the project root, so it belongs in a project's .claude/settings.json. In your user settings or managed settings, use the script's absolute path instead, such as ${HOME}/.claude/hooks/block-destructive.sh: a project-relative path there resolves against whichever project you are in, the script is missing, and because onFailure is block, every command the hook matches is blocked.
The matcher names the tool that runs the command: Bash or PowerShell. Use Bash|PowerShell unless you deliberately want to cover one shell. args is empty because the script takes no arguments; keep the field and list them if yours does.
The script reads the hook's JSON on standard input. To block, print a reason to standard error and exit 2:
#!/usr/bin/env bash
command -v node >/dev/null || { echo "node is missing, so the command cannot be checked" >&2; exit 2; }
input=$(cat)
cmd=$(printf '%s' "$input" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>console.log(JSON.parse(s).tool_input.command))')
case "$cmd" in
*"rm -rf"*) echo "Destructive command blocked by hook" >&2; exit 2 ;;
esac
exit 0The script uses node to read the JSON, so the first line makes a machine without it fail closed and block, rather than passing the command unchecked. To log a command instead of blocking it, print it and exit 0; an allow decision changes nothing.
onFailure: "block" matters: without it, a hook that fails or times out lets the command run. In a Code Mode session where Claude has already run code, a timeout blocks the command by default.
On Windows, replace command and args with the form in the Windows tab of Anthropic's hook-resolution section. Exec form there needs command to resolve to a real executable such as a .exe, so the .sh above cannot run as written.
What the hook receives
The input carries the common hook fields, including permission_mode, plus these:
| Field | What it holds |
|---|---|
tool_name | Bash or PowerShell, the tool that runs the command. |
tool_input.command | The shell command as it would run. |
tool_use_id | The command's ID. It names no tool call in the transcript. |
command_name | The skill or command loading, such as pr-summary, or my-plugin:deploy for a plugin's skill. |
command_source | Where it comes from, such as userSettings, projectSettings, policySettings, plugin or builtin. |
invoked_by | user when you typed the skill's name or an Agent SDK app sent it, claude when Claude or a session-scoped scheduled task invoked it, and other for anything else, such as a subagent that preloads the skill. |
What each answer does
| What the hook returns | What happens |
|---|---|
Exit 0 with no decision, or permissionDecision: "allow" | The command runs unless another hook blocks it. |
Exit 2, permissionDecision: "deny", or a top-level decision: "block" | The command does not run. The skill still loads, and Claude reads [not run: blocked by hook: <reason>] in place of its output. |
permissionDecision: "ask" or "defer", or any updatedInput | Claude Code blocks the command, the same as a deny. This hook can block a command but cannot approve or change one. |
continue: false | Invoking the skill fails with Operation stopped by hook: <stopReason>. None of its injected commands runs, because every hook's answer is collected before the first one runs. |
| A command, HTTP or MCP tool hook that fails or times out | The command runs unless the hook sets onFailure: "block", or, in a Code Mode session (where Claude runs its tool calls from a script) after code has already run, a timeout blocks it by default. |
For JSON, use the PreToolUse decision shape. A script shared with PreToolUse can leave hookEventName as PreToolUse; one written only for this event sets it to PreSkillShellCommand. Put the reason in permissionDecisionReason, or in reason beside a top-level decision. While Remote Control is connected, the hook still blocks but neither you nor Claude sees the reason; it is in the debug log.
How to test it without deleting anything
Anthropic's own test is a harmless echo. Create .claude/skills/hook-test/SKILL.md holding this one line:
!`echo 'rm -rf'`Start a session and type /hook-test. A working block prints Not run in /hook-test: echo 'rm -rf' (blocked by hook: Destructive command blocked by hook). The echoed text is a read-only command, so nothing is deleted by the test.
When you type a skill's name, a Not run in /<name> line reports each blocked command. A Ran anyway in /<name> line reports a command that ran after its hook failed or timed out, which is the sign that onFailure is missing.
Common mistakes
- Putting the hook in the skill's frontmatter. A skill's injected commands run without the hooks in its own frontmatter on its first load in a session and on loads inside a subagent, and a forked skill's commands run without the hooks in its own frontmatter or in its subagent's definition. Use a settings file or a plugin.
- Using a prompt or agent hook. The event takes command, HTTP and MCP tool hooks. Claude Code cannot run a prompt or agent hook here, so it blocks every command the hook matches.
- Making the hook async. An async hook cannot block.
- Expecting the skill to abort. A deny stops one command and the skill still loads. Use
continue: falseto stop the invocation. - Relying on this event alone in auto mode. A command held from loading can run afterward as a tool call, which fires
PreToolUse. Keep the script on both events.
Where these answers come from
- Anthropic's hooks reference, the PreSkillShellCommand section, read November 27, 2026: when it runs, the matcher, the hook types, the settings example, the test, the input fields, the exit-code table, and the Remote Control note.
- Anthropic's skills page, inject dynamic context, read November 27, 2026: the
!`command`and```!forms, the permission check, and the statement thatPreToolUsedoes not fire for them. - Anthropic's changelog, release 2.1.332, read November 27, 2026: the event's introduction and the dialog an older version opens.
Common questions
What is the PreSkillShellCommand hook?
It is a hook event that runs before an injected command: a shell command written inside a skill or a slash command, which runs while that skill or command loads. It can log the command or block it. Anthropic asks for Claude Code v2.1.332 or later for it.
Why does my PreToolUse hook not fire for a skill's commands?
Injected commands are not tool calls. Claude Code runs them while the skill loads, after its own permission check, and PreToolUse hooks do not fire for them. Add the same script to the PreSkillShellCommand event to see or block them.
Does blocking a command stop the skill from loading?
No. A block stops that command only: the skill still loads, and Claude reads a not-run line in place of the command's output. To stop the whole invocation, return continue false, which fails the skill with Operation stopped by hook.
Can I use a prompt or agent hook on this event?
No. The event takes command, http and mcp_tool hooks, and a prompt or agent hook configured here cannot be run at all, so Claude Code blocks every command the hook matches. An async hook is no use either, because it cannot block.