Claude Code PreSkillShellCommand: Block the Shell Commands a Skill Runs as It Loads#
Published by Alex Sorokoletov
PreSkillShellCommand is a Claude Code hook event, added in v2.1.332, that runs before an injected command: a shell command that a skill or a slash command such as /security-review runs while it loads. PreToolUse hooks never fired for these, so a skill could run shell as it loaded without passing your PreToolUse script. To block them, add a PreSkillShellCommand entry in a settings file with a Bash|PowerShell matcher and onFailure: "block".[1]
What an injected command is#
A skill or slash command can carry shell commands in its own file that run as the skill loads, before Claude takes a turn. In a skill's file they appear as !`<command>` or inside a ```! block. Claude Code calls these injected commands. They run through the Bash or PowerShell tool as the skill loads, and until v2.1.332 no hook event saw them, so a PreToolUse script that blocks a command for a normal tool call did not apply here.[2]
If you already run a PreToolUse script to log or block commands, add the same script to PreSkillShellCommand to cover skills and commands as they load.
Requirements#
PreSkillShellCommand needs Claude Code v2.1.332 or later, in the terminal, the desktop app and the VS Code extension. Update before you add the hook, because earlier versions run injected commands without calling it.[2]
If you add the event to a settings file on an older build, v2.1.331 and earlier open each interactive terminal session with a warning dialog: Unknown hook event "PreSkillShellCommand" was ignored. To require a minimum version from the settings file itself, use requiredMinimumVersion.[2]
How to block injected commands#
Define the hook in a settings file (managed, project, or user settings) or in a plugin. A skill's injected commands run without the hooks in the skill's own frontmatter on its first load in a session and on loads inside a subagent, so a hook placed only in a skill cannot guard that skill.[2]
For a hook that has to block, set it up this way:[2]
- Matcher:
Bash|PowerShell, to cover both tools that run injected commands. - Type: a
command,http, ormcp_toolhook that is not async, because an async hook cannot block. Apromptoragenthook on this event cannot run, and Claude Code blocks every command it matches. - Failures:
onFailure: "block", so the command does not run when the hook fails or times out.
On macOS and Linux, this entry in .claude/settings.json runs a blocking script:
{
"hooks": {
"PreSkillShellCommand": [
{
"matcher": "Bash|PowerShell",
"hooks": [
{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh", "args": [], "onFailure": "block" }
]
}
]
}
}
To test it, create .claude/skills/hook-test/SKILL.md with the single line !`echo 'rm -rf'`. That read-only command needs no approval and deletes nothing. Start a terminal session and type /hook-test. With a script that blocks it, you see Not run in /hook-test: echo 'rm -rf' (blocked by hook: <reason>).[2]
What the hook receives#
Besides the common input fields, which include permission_mode, a PreSkillShellCommand hook gets these fields:[2]
| Field | Holds |
|---|---|
tool_name | The tool that runs the command, Bash or PowerShell. |
tool_input.command | The shell command as it would run. |
tool_use_id | ID of this command, which names no tool call in the transcript. |
command_name | Name of the loading skill or command, such as pr-summary, or my-plugin:deploy for a plugin's skill. |
command_source | Where it comes from: 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, "other" for anything else, such as a subagent that preloads the skill. |
The script runs after the permission check on injected commands, so it gets the commands that passed, and it runs in bypassPermissions mode. In auto mode, a command that would otherwise need approval does not run while the skill loads; Claude can run it afterward as a tool call, which fires PreToolUse and not this event. Keep your script on both events.[2]
How to block, and what a block leaves behind#
Exit 2 or return permissionDecision: "deny" to block an injected command. You can block a command from this hook, but you cannot approve or change one.[2]
For a JSON response, use the PreToolUse format. To run one script on both events, leave hookEventName as PreToolUse. For a script that runs only here, you can set hookEventName to PreSkillShellCommand.[2]
| Hook outcome | What happens |
|---|---|
Exit 0 with no decision, or permissionDecision: "allow" | The command runs unless another hook blocks it. |
Exit 2, permissionDecision: "deny", or 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 the command's output. |
permissionDecision: "ask" or "defer", or any updatedInput | Claude Code blocks the command the same way as deny; a skill cannot wait for approval, and the command cannot be changed here. |
continue: false | Invoking the skill fails with Operation stopped by hook: <stopReason>. None of its injected commands runs, because Claude Code collects every hook answer before it runs the first command. |
A command, http, or mcp_tool hook fails or times out | The command runs unless the hook is one whose failure blocks the action, for example with onFailure: "block". In a Code Mode session where Claude has already run code, a timeout blocks the command by default. |
When you type a skill's name in the terminal, a Not run in /<name> line reports a blocked command, and a Ran anyway in /<name> line reports a command that ran after its hook failed or timed out.[2]
To give a reason for a block, write it to stderr when you exit 2, or put it 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; find it in the debug log.[2]
Why this event exists#
Skills and slash commands can come from a plugin or a marketplace, and a skill file can carry shell that runs the moment the skill loads, before Claude's first turn. Until v2.1.332, those commands went through the Bash or PowerShell tool without reaching a PreToolUse hook, so a policy that blocked a command for ordinary tool calls did not cover a skill loading it. PreSkillShellCommand closes that gap: a managed or project settings file can now vet or refuse each injected command by the same rules it applies to the rest of the session.[1]
Timeline#
| Version | Published | Change |
|---|---|---|
| v2.1.331 and earlier | through 2026-11-23 | Injected commands run with no hook event. A settings file that names PreSkillShellCommand opens each interactive session with the dialog Unknown hook event "PreSkillShellCommand" was ignored. |
| v2.1.332 | 2026-11-24 | The PreSkillShellCommand hook event ships. A hook can refuse a skill's or command's inline shell commands as it loads. |
FAQ#
Q: What is PreSkillShellCommand in Claude Code?
A: A hook event, added in v2.1.332, that runs before an injected command: a shell command a skill or slash command runs as it loads. A hook on this event can block the command before it runs.
Q: Which version added PreSkillShellCommand?
A: v2.1.332, published November 24, 2026. On v2.1.331 and earlier, a settings file that names the event opens each interactive terminal session with the dialog Unknown hook event "PreSkillShellCommand" was ignored.
Q: Why did my PreToolUse hook not block a command a skill ran?
A: PreToolUse does not fire for injected commands, the shell a skill or slash command runs as it loads. Add the same script to PreSkillShellCommand to cover that case.
Q: Can a PreSkillShellCommand hook approve or change a command?
A: No. It can only block. permissionDecision: "ask" or "defer", and any updatedInput, are all treated as a block, because a skill cannot wait for approval and the command cannot be changed here.
Q: Where do I put a PreSkillShellCommand hook?
A: In a settings file (managed, project, or user settings) or in a plugin, with a Bash|PowerShell matcher. A skill's injected commands run without the hooks in the skill's own frontmatter on first load and inside a subagent, so a hook in the skill cannot guard it.
Q: How do I block an injected command when the hook itself fails?
A: Set onFailure: "block" on the hook, and use a command, http, or mcp_tool hook, not an async or prompt or agent hook. An async hook cannot block, and a prompt or agent hook cannot run on this event.
Sources: [1] Claude Code changelog, v2.1.332 | [2] Claude Code hooks reference
Related: Why Claude Code hooks are not running in this session | Can an MCP server tell Claude Code to hide things? | Claude Code "Don't ask again" saved a password | Claude Code versions