claude -p runs a repo's hooks without asking: use --bare in scripts

Without --bare, claude -p runs the repo's hooks and connects its MCP servers even if you never trusted it, and asks nothing. In scripts and CI, run it with --bare and name the tools.

Published 3 min read#AI#Developer tools#App security

One command in your CI that asks Claude Code to review a pull request can run code from the repository itself before Claude reads a single line. That code sits in settings files inside the repository, and the command loads them on its own. By the end of this article you will know what claude -p loads from the folder, and how to run it in a script so that it loads only what you name.

What is claude -p?

The claude -p command is Claude Code without a screen: it takes the request from the command line, does the work, prints the result, and exits. That is how you use it in a script or in CI.

According to the docs on running Claude Code programmatically, the -p flag (or --print) runs any claude command non-interactively, and the command exits with code 0 on success and a non-zero code on failure, so your script can branch on the exit status.

What does claude -p load from the repo on its own?

Without --bare, claude -p loads from the folder everything an interactive session would load, including hooks and MCP servers. The difference is that nobody asks you first.

The same docs page says that a -p session without --bare runs the hooks in the project's .claude/settings.json and connects the servers in its .mcp.json, even in a folder you have never trusted. And the session shows no workspace trust dialog and no per-server approval prompt.

According to the hooks documentation, a hook is something the user defines (a shell command, for example) that runs automatically at specific points in Claude Code's work (details in the Claude Code hooks article). So if your CI checks out the pull request author's branch and runs claude -p on it, the branch author is the one who wrote those files.

What does --bare do?

The --bare flag runs Claude Code without discovering anything in the folder on its own: no hooks, no MCP servers, no CLAUDE.md file. Whatever you need, you pass with an explicit flag.

According to the docs, --bare skips auto-discovery of hooks, skills, custom commands, subagents, plugins, MCP servers, auto memory and CLAUDE.md. It still reads settings files and their permission rules. And the docs say --bare is the recommended mode for scripted calls, and will become the default for -p in a future release.

claude --bare -p "Summarize README.md" --allowedTools "Read"

This mode does not use your subscription login, so you set ANTHROPIC_API_KEY in the environment before you run it, as the docs say.

How do you limit what it can do?

You limit what Claude can do with the --allowedTools flag, which allows named tools without asking, and with a permission mode that denies every call that would otherwise ask. That way the script never hangs waiting for approval. Calls that need no approval still run even if you did not name them, such as file reads in your working directories and the read-only command set.

claude --bare -p "Review the staged changes for bugs" \
  --allowedTools "Read,Bash(git diff *)" \
  --permission-mode dontAsk \
  --output-format json
  1. --allowedTools "Read,Bash(git diff *)" allows reading and the git diff command only. The space before the star matters: according to the docs, Bash(git diff*) without the space also matches git diff-index.

  2. --permission-mode dontAsk denies every call that would otherwise prompt, which is useful for locked-down CI runs.

  3. --output-format json returns the result as JSON, with the text in the result field. The other two options are text (the default) and stream-json.

How do you read the result in a script?

You read the result from the result field of the JSON, and you check the exit code before you use the text. A non-zero code means the run failed, so the script should stop.

if ! claude --bare -p "Summarize this project" --output-format json > out.json; then
  echo "claude run failed" >&2
  exit 1
fi
jq -r '.result' out.json

And the result itself comes from a model that read files someone else may have written, so do not run it as a command or put it somewhere sensitive without checking it (see prompt injection in agent tools).

Where do you start today?

Search your scripts and CI files for every line with claude -p, and add --bare and an --allowedTools list with only the tools it needs, because the repository it works on may not be yours. And if you are building your team's work on coding agents, the AI skills section has skills for Claude Code and coding agents. How many claude -p lines did you find?

FAQ

Does --bare work with my regular Claude subscription login?

No. This mode does not use your subscription login, so you set ANTHROPIC_API_KEY in the environment before you run it.

Does --bare ignore the permission rules in my settings files?

No. The docs say it still reads settings files and their permission rules, but a hook or an MCP server from the repo does not run.

Why is there a space before the star in Bash(git diff *)?

Because without the space the rule also matches other commands that start with the same letters, like git diff-index. The space makes it match git diff with or without arguments.

Share

More articles