# Claude Code headless print mode with claude -p

Canonical: https://cwyn.com/pages/claude-code-guide-headless-print-mode.html
Last updated: 2026-11-27T04:10:33-05:00

Claude Code setup guide

Run `claude -p "your prompt"` to print one reply and return control to the shell. Plain text is the default, and you can pipe input into the command. For JSON, add `--output-format json` and check the exit status before reading the reply from the `result` field.

Last updated November 27, 2026 · Published November 19, 2026

By CWYN · Published November 19, 2026 · Not affiliated with Anthropic

## 1. Run a single prompt

From a Bash or Zsh terminal with Claude Code installed, try the command below. Print mode runs without the interactive interface. `--bare` skips discovery of customizations such as [hooks](https://cwyn.com/pages/claude-code-guide-hooks-examples.html), MCP servers and `CLAUDE.md`. For the Anthropic API, set `ANTHROPIC_API_KEY` in the environment before running these examples. Bare mode requires API authentication for this setup.

```
claude --bare -p --tools "" "Rewrite this as a status update: the export button is ready."
```

## 2. Pipe text into the prompt

Pass sample text on standard input and put the task in the prompt. The empty `--tools` list disables the built-in tools. These bare runs supply no [MCP servers](https://cwyn.com/pages/claude-code-guide-mcp-setup.html).

```
printf '%s\n' "CSV export is ready. Documentation is still in review." |
  claude --bare -p --tools "" "Summarize the input in one sentence."
```

## 3. Save the result as JSON

The first two commands use the default output format, `--output-format text`. Add `--output-format json` so a script can read the structured response. This example writes it to `summary.json` in the current directory. Use a new filename if you have a file there you want to keep. For parallel runs, see [how to give each session its own worktree](https://cwyn.com/pages/claude-code-guide-worktrees.html).

```
printf '%s\n' "CSV export is ready. Documentation is still in review." |
  claude --bare -p --tools "" --output-format json "Summarize the input in one sentence." > summary.json
```

## 4. Check exit status before reading the reply

Have your script check the exit status before it reads the file. Claude Code exits with a nonzero status on failure, and a failure during the run can appear on standard output. After a successful run, use `jq -r '.result' summary.json` if you have jq installed. Check that the reply preserves both details from the input.

For recurring scripts, [compare a Claude subscription with API billing](https://cwyn.com/pages/claude-code-guide-subscription-vs-api.html). To budget the same coding workload across providers, see [Claude Code and Codex costs](https://cwyn.com/pages/claude-code-guide-claude-code-vs-codex-cost.html).

To run a prompt when a pull request opens, [set up Claude Code GitHub Actions](https://cwyn.com/pages/claude-code-guide-github-actions.html).

Checked against docs, not run. November 27, 2026 · [Print mode, piped input, bare mode authentication, JSON and exit status](https://code.claude.com/docs/en/headless) · [Built-in tool restriction and print mode flags](https://code.claude.com/docs/en/cli-reference)

## Related guides

- [Why does Claude Code still ask after I allow a command?](https://cwyn.com/pages/claude-code-guide-permission-allowlist-prompts.html)
- [Create a Claude Code slash command with a project skill](https://cwyn.com/pages/claude-code-guide-first-project-skill.html)
- [Install Claude Code on Windows with PowerShell](https://cwyn.com/pages/claude-code-guide-windows-powershell-install.html)
- [How much does a Claude Code session cost?](https://cwyn.com/pages/claude-code-guide-cost-per-session.html)

## Next steps

- [All 34 Claude Code guides](https://cwyn.com/guides/claude-code/)
- [Claude Code setup walkthrough](https://cwyn.com/setup/claude-code/)
- [Check your CLAUDE.md](https://cwyn.com/score/setup-file-checker/?profile=claude)
- [Claude Code Complete Setup Pack ($12.95)](https://cwyn.com/pages/pack-claude-code-complete-setup-pack.html)

### Set up your Claude Code project

If you also run Claude Code interactively, the [Claude Code Complete Setup Pack ($12.95)](https://cwyn.com/pages/pack-claude-code-complete-setup-pack.html) includes project instructions, hooks and permission rules for those sessions.
Kit details as of Nov 27, 2026

Claude Code Complete Setup Pack $12.95

Set up project instructions, permission rules, hooks, a reviewer skill and subagent, and MCP connections for Claude Code.
