A rule that works in a session and not in a script
In an interactive session, an allow rule in .claude/settings.json does what it says: the command runs without a question. So when I moved the same task into a script with claude -p, I expected the same behavior. The command was refused instead.
I first saw this while testing a recipe for the GitHub CLI, where gh pr list was refused although the rule for it was in the project file. For this article I reduced it to the smallest project that shows the effect, and ran it again with Claude Code 2.1.335.
The smallest project that shows it
A new folder with two scripts. One only reads, and one changes something:
# report.sh
echo "orders today: 42"
# deploy.sh
echo "deployed" > deployed.txt
And a settings file that allows the first and denies the second:
{
"permissions": {
"allow": ["Bash(./report.sh)"],
"deny": ["Bash(./deploy.sh)"]
}
}
Then the headless run, in that folder:
claude -p "Run ./report.sh and tell me what it printed." \
--output-format json --debug-file /tmp/claude.log
The answer:
./report.sh was refused: the Bash call came back with "This command
requires approval," so the script did not run and printed nothing.
The line in the debug log
The debug log has the reason near the top, before the model does anything:
Dropped 1 project-scoped permissions.allow entry β workspace not yet trusted
Applying permission update: Adding 1 deny rule(s) to destination
'projectSettings': ["Bash(./deploy.sh)"]
Claude Code read the file. It kept the deny rule and dropped the allow rule, because nobody had accepted the trust dialog for this folder. An interactive session shows that dialog the first time you start claude in a folder. A headless run cannot show a dialog, so in a folder that was never opened interactively, the question was never answered.
Why this is the right behavior
Think of a repository that you cloned from a stranger. If its settings file could allow commands for itself, the file could contain "allow": ["Bash(*)"], and the first claude -p in that clone would run whatever the model is talked into. So Claude Code separates the two kinds of rules:
- A rule that restricts (
deny,ask) is applied at once. The worst a foreign file can do with it is to forbid something. - A rule that grants (
allow) waits until a person has said that they trust the folder.
The same applies to the other grants in a project file. In my tests for the book, MCP servers that the project's settings named as approved were still listed as pending in a fresh clone, and their tools were refused in a headless run.
The run does not fail, which is the real trap
Look at what the script received from the refused run:
{
"is_error": false,
"permission_denials": [
{ "tool_name": "Bash", "tool_input": { "command": "./report.sh" } }
]
}
The exit code was 0 and is_error was false. From the point of view of Claude Code the run succeeded: the model was refused a tool, said so, and finished. A script that only checks the exit code continues with an answer that says "I could not run it".
So a script that depends on a tool call has to check the denials too:
out=$(claude -p "$prompt" --output-format json)
if [ "$(jq '.permission_denials | length' <<<"$out")" -gt 0 ]; then
echo "A tool call was refused:" >&2
jq -r '.permission_denials[].tool_input.command' <<<"$out" >&2
exit 1
fi
Two fixes that I tested
1. Name the tools on the command line.
claude -p "Run ./report.sh and tell me what it printed." \
--allowedTools "Bash(./report.sh)"
Result: orders today: 42, and an empty permission_denials list. This is what I use in scripts and in CI. The permission is in the same place as the command, where a reviewer sees it.
2. Pass the settings file yourself.
claude -p "Run ./report.sh and tell me what it printed." \
--settings .claude/settings.json
Same result. A file that you name on the command line counts as your decision and not as the repository's, so its allow rules are applied. This is convenient when the list of rules is long. It also removes the protection: read the file before you do this in a repository that you did not write.
There is a third way that I did not run again for this article: start claude once in the folder and accept the trust dialog. That is what makes the rules work in an interactive session. It does not help on a CI machine, where every run starts from a fresh clone.
The deny rule wins, even against the command line
The other half of the file deserves a test too. I allowed the forbidden script on the command line, which is the strongest thing a caller can do:
claude -p "Run ./deploy.sh." --allowedTools "Bash(./deploy.sh)"
The answer:
The command was refused. The permission system denied ./deploy.sh,
so I stopped and did not retry it.
deployed.txt was not created. The deny rule from the project file was applied in the untrusted folder, and it won against my own --allowedTools. That is the useful side of the same design: a team can put its prohibitions into the repository, and they hold in every run, with or without trust.
How to check your own script
- Run it once with
--debug-file /tmp/claude.logand search the log forDropped. The line tells you how manyallowentries were held back. - Add
--output-format jsonand look atpermission_denials. An empty list means that nothing was refused. - Move every permission that the run needs to
--allowedTools, and keep onlydenyrules in the project file for headless use.
All four runs in this article were single runs with the Haiku model. The refusals come from the permission system and not from the model, so I expect the same with every model, but I did not test the others.
Where this comes from
This is one of the findings from testing the recipes of my book, The Claude Code Automation Cookbook: 40 recipes for hooks, subagents, skills, MCP and CI workflows, each one run in a real session before it was written down. The code for all of them is in the GitHub repository, with a test for every script. The book is available on Amazon as paperback and for Kindle.
The previous article in this series: My Claude Code formatting hook was correct, and it never ran.
Author Amer Malik Mohammed
Full-Stack Developer with 2+ years of experience in object-oriented PHP development (Symfony), JavaScript and MySQL. Specialized in e-commerce solutions (Shopware 5/6), REST API development and process automation in agile teams.
Contact author