On 24 November 2026 we searched Anthropic's Claude Code repository on GitHub for the text PreToolUse:Bash hook error and got 79 issues and pull requests. People paste it when a hook misbehaves: a wrong path, a missing execute bit, or a hook that exits 1 and expects to block.
A PreToolUse hook that answers in plain text stops the command only when it exits with 2, and then Claude gets the hook's message as the reason. Exit 1 only logs a non-blocking error, and the command runs anyway.
A PreToolUse hook is a script Claude Code runs before a tool call; here, before every Bash command. It reads the planned call as JSON on standard input. It answers with its exit code, and it can add a message on standard error.
I wanted the smallest hook that shows the difference, so we built a script that refuses rm -rf and ran it twice in a fresh folder: once exiting 2, once exiting 1. Everything below is copied from those runs on Claude Code 2.1.330, macOS 27.2.1, jq 1.7.1.
Note
Prerequisites: Claude Code installed and signed in (
claude --versionshould print a version; ours was 2.1.330).jqon your PATH: macOS ships it as/usr/bin/jq, on Linux install it with your package manager. A terminal with bash. On Windows, run this under Git Bash: the hooks reference says that without Git Bash a failing hook can block the call too, so exit 1 would not behave as shown here. You'll need about 10 minutes.
PreToolUse hook exit code 2 vs exit 1: what each does
The hooks reference sets out three outcomes for a command hook:
| Exit code | What happens to the tool call | Who sees the hook's stderr |
|---|---|---|
| 0 | Goes ahead (unless JSON on stdout denies it, a second way to answer that this tutorial doesn't cover) | Not Claude; you can read it in the debug log |
| 2 | Blocked | Claude, as the reason |
| Anything else | Goes ahead | You, as a non-blocking error in the transcript |
So a script that ends with exit 1 because it "failed" lets the command through. I think this is the easiest of the three rows to get wrong. Our second run below shows it.
The same rule covers a hook that never starts. The reference documents a setting for that case, "onFailure": "block" on the hook entry (Claude Code 2.1.295 or later), which makes a failed hook block the call instead of letting it through. We did not run that setting for this article, so I can only point you to the reference for it.
Build the hook
[[steps]]
-
Create the project — in a terminal, make an empty folder with a
builddirectory to delete. Result:ls buildprintsapp.js. -
Write the hook script — save
.claude/hooks/block-rm.sh, then make it executable in the same terminal. Result:ls -lshowsxin its permissions. -
Register it in .claude/settings.json — a
PreToolUseentry with the matcherBash. Result: thejqcheck printsvalid json. -
Test the script without Claude — pipe one line of JSON into it. Result: the message and
exit=2forrm -rf build, andexit=0forls build. -
Start Claude and ask for rm -rf — start
claudein the folder, then type the request. Result: the tool call comes back blocked with your message. -
Build a second copy that exits 1 — a new folder with the same hook ending in
exit 1. Result:grepshowsexit 1. -
Ask again in the second copy — the same request. Result: the command runs and
buildis deleted.
Step 1: Create the project
- In a terminal, run:
mkdir hooks-demo && cd hooks-demo
mkdir -p .claude/hooks build
echo "old artefact" > build/app.js
- Check it:
ls buildshould printapp.js.
Step 2: Write the hook script
- Save this as
.claude/hooks/block-rm.sh:
#!/bin/bash
# PreToolUse hook: refuse any Bash command that contains "rm -rf".
cmd=$(jq -r '.tool_input.command // empty')
if [[ "$cmd" == *"rm -rf"* ]]; then
echo "Blocked by block-rm.sh: 'rm -rf' is not allowed in this project. Delete files one by one instead." >&2
exit 2
fi
exit 0
jq -r '.tool_input.command // empty' reads the command Claude wants to run from the JSON on standard input. The // empty part makes it print nothing instead of the word null when the field is missing, so a call without a command falls through to exit 0. The message goes to standard error (>&2), because that is the stream Claude Code hands to Claude on exit 2.
- Make it executable:
chmod +x .claude/hooks/block-rm.sh
- Check it:
ls -l .claude/hooks/block-rm.shshould show-rwxr-xr-xor another mode withxin it.
Step 3: Register the hook in .claude/settings.json
- Save this as
.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-rm.sh"
}
]
}
]
}
}
The matcher filters by tool name. This hook runs before Bash calls (and, per the reference, before Monitor commands); other tools skip it.
$CLAUDE_PROJECT_DIR is the folder the session started in, which keeps the path right even when Claude has moved into a subfolder. The quotes around it protect a path with spaces.
The settings page lists where else a hook can live: ~/.claude/settings.json for every project on your machine, .claude/settings.local.json for you alone in this one.
- Check it parses. With one stray comma Claude Code shows a Settings Error at start, and if you continue past it the hook isn't loaded:
jq . .claude/settings.json > /dev/null && echo valid json
The check should print:
valid json
Step 4: Test the script without Claude
Claude Code sends the hook a JSON object with tool_name, tool_input and a few session fields. You can fake the two that matter.
- Run:
echo '{"tool_name":"Bash","tool_input":{"command":"rm -rf build"}}' | ./.claude/hooks/block-rm.sh; echo "exit=$?"
echo '{"tool_name":"Bash","tool_input":{"command":"ls build"}}' | ./.claude/hooks/block-rm.sh; echo "exit=$?"
- You should see:
Blocked by block-rm.sh: 'rm -rf' is not allowed in this project. Delete files one by one instead.
exit=2
exit=0
It takes less than a second. I'd run it after every change to a hook, because it catches a broken one before Claude is involved. Here is one failure reproduced the same way in zsh, the macOS default shell:
chmod -x .claude/hooks/block-rm.sh
echo '{"tool_input":{"command":"rm -rf build"}}' | ./.claude/hooks/block-rm.sh; echo "exit=$?"
chmod +x .claude/hooks/block-rm.sh
zsh: permission denied: ./.claude/hooks/block-rm.sh
exit=126
In bash and /bin/sh, the shell the hooks reference says Claude Code runs hooks with, the message is Permission denied, and a typo in the script's path gives No such file or directory and exit=127. zsh prints both in lowercase.
Neither 126 nor 127 is 2, so under Claude Code both count as non-blocking errors and the command runs.
Step 5: Start Claude and ask for rm -rf
- Start Claude Code in the folder:
claude
If Claude Code asks whether you trust the folder, accept. Our sessions ran in Claude Code's auto permission mode (the mode where Claude runs commands without asking each time; Shift+Tab cycles the modes). In the default mode the hooks reference puts the PreToolUse hook before the permission prompt, so the block arrives without a question.
Type:
Run this exact command with the Bash tool: rm -rf build
- Check the result. Claude makes the call, and the session transcript (the file
~/.claude/projects/<project folder>/<session id>.jsonlthat Claude Code writes for every session) records this as the tool result:
PreToolUse:Bash hook error: ["$CLAUDE_PROJECT_DIR"/.claude/hooks/block-rm.sh]: Blocked by block-rm.sh: 'rm -rf' is not allowed in this project. Delete files one by one instead.
The blocked call carries the label "hook error" too. That line is the constant part. What Claude does next is its own choice, and it differed between our two runs of this step.
In the first, Claude listed what was in build, then ran rm build/app.js && rmdir build && ls -la, and ended its reply with:
`build` is deleted, but I didn't run your exact `rm -rf build`. A hook in this project, `.claude/hooks/block-rm.sh`, blocks any `rm -rf` and says to delete files one at a time.
In the second, Claude ran no further command, said nothing in build had been deleted, and asked which option we wanted, deleting the files one by one among them. In both runs the rm -rf itself never ran. Claude may follow the hook's message or stop and ask. So I'd write the message as the instruction you want carried out. Want nothing deleted? Say so.
Step 6: Build a second copy that exits 1
Using a second folder leaves the exit-2 copy as it is, so you can compare the two.
- Run:
cd .. && mkdir hooks-exit1 && cd hooks-exit1
mkdir -p .claude/hooks build && echo "old artefact" > build/app.js
cp ../hooks-demo/.claude/settings.json .claude/
sed 's/exit 2/exit 1/' ../hooks-demo/.claude/hooks/block-rm.sh > .claude/hooks/block-rm.sh
chmod +x .claude/hooks/block-rm.sh
grep -n exit .claude/hooks/block-rm.sh
- You should see:
6: exit 1
8:exit 0
Step 7: Ask again in the second copy
Start
claudeinhooks-exit1and send the same request. We ran in auto mode. In the default mode the permissions docs say Claude Code asks before a command like this, so you would approverm -rf build, and approving it deletesbuild.Check the result. The Bash call returned
(Bash completed with no output), and the session transcript stored the hook's run as this record (trimmed to the fields that matter):
{
"type": "hook_non_blocking_error",
"hookName": "PreToolUse:Bash",
"hookEvent": "PreToolUse",
"exitCode": 1,
"stderr": "Failed with non-blocking status code: Blocked by block-rm.sh: 'rm -rf' is not allowed in this project. Delete files one by one instead."
}
Claude's reply began: "I ran rm -rf build ... It printed nothing and reported no errors". It never saw the message.
- Confirm the folder is gone:
lsinhooks-exit1should print nothing.
Exit 1 looks like an error to you and like "carry on" to Claude Code.
Reading a PreToolUse:Bash hook error: failed with non-blocking status code
If you meet PreToolUse:Bash hook error in someone else's setup, read what comes after the label.
When the hook's own message follows with no prefix, as the tool result, the hook exited 2 and the call was stopped.
When Failed with non-blocking status code: sits in front, the hook exited with something else and the command ran.
And /bin/sh: ...: Permission denied or No such file or directory after that prefix means the hook never got to its logic. I'd check the path in settings.json first. Then the x bit.
To confirm a fix, run the Step 4 pipe test against that hook: it should print the hook's message and exit=2 for a command it means to block. I'd also type /hooks inside Claude Code to see which hooks are registered and from which file. The settings page says edits to the settings files are picked up by the running session; if a change does not show up after a few seconds, restart Claude Code.
The same question, what an AI client may do before it acts, comes up with the Atlassian MCP server's permissions, where Atlassian's own admin controls decide what an AI client may write. If you're preparing for a Claude Certified exam (Associate, Developer or Architect), our free practice exams are here.
Clean up
cd .. && rm -r hooks-demo hooks-exit1
[[takeaways]]
- You have a project hook in
.claude/settings.jsonthat blocks any Bash command containingrm -rf, and a one-line test that proves it before Claude runs. - Exit 2 blocks and gives Claude your stderr as the reason. Exit 1, 126 and 127 log a non-blocking error and the command runs.
- After a block, Claude may follow the message or stop and ask, so write the message as the instruction you want carried out.
- Not covered here: hooks that answer with JSON (
permissionDecision),onFailure: "block", and the other hook events such asPostToolUseandStop.
Originally published on leanzero.net. More Atlassian, Forge and local-AI write-ups at leanzero.net/blog, and if you're planning a migration or a Forge app, that's what we do: leanzero.net/services.


Top comments (0)