PrepGenAICerts

-p / --print Flag

Claude Code

Definition

Claude Code CLI flag for non-interactive (headless) execution. Processes the prompt, outputs to stdout, and exits immediately without entering an interactive session. Essential for CI/CD pipeline integration, scripting, and automation workflows.

Example Usage

claude -p 'Review this PR for security issues' --output-format json in a GitHub Actions workflow step.

In Depth

The -p / --print flag switches Claude Code from its default interactive REPL into a single-shot, headless mode. Pass it a prompt string, and Claude Code processes that prompt, writes the response to stdout, then exits with a zero status code on success (or non-zero on error). No TTY is required; no interactive session is opened; the process lifecycle is entirely predictable.

This behavioral change is what makes Claude Code composable with the rest of the shell ecosystem. A CI step, a cron job, a Git hook, or a Make target can invoke claude -p '...' and treat it like any other subprocess: capture stdout, inspect the exit code, pipe the output to the next stage. Without --print, Claude Code would hang waiting for interactive input in environments that have no terminal — a failure mode that can be surprisingly hard to diagnose when the process is wrapped inside a job runner.

The flag pairs naturally with --output-format to control how the response is structured. --output-format text (the default) is appropriate for human-readable summaries in GitHub Actions logs. --output-format json emits a structured envelope that downstream tooling can parse without fragile regex matching. --output-format stream-json emits newline-delimited JSON events as the model generates them, which is useful for long-running tasks where you want to update a progress display in real time rather than wait for the full response.

From a security standpoint, --print combined with a scoped allowed-tools configuration gives you an auditable, reproducible invocation. Every CI run with the same flags and the same prompt produces a traceable artifact: what Claude was asked, what tools it was permitted, and exactly what it returned. This is a meaningful improvement over ad-hoc scripting because the model's behavior is scoped and logged.

A common mistake is omitting --print in a shell script and then wondering why the script hangs. Another is forgetting that --print still runs the full model inference — it is not a "fast path" that skips reasoning. It changes the interaction model (non-interactive) not the compute model. Latency in --print mode is the same as latency in interactive mode for an equivalent prompt.

For exam scenarios, remember that --print is also required when you want to chain Claude Code inside a shell pipeline: claude -p 'Summarize this PR' | jq '.summary' only works because --print ensures Claude Code terminates after producing its output rather than entering an interactive loop. The CI/CD Integration with Claude Code concept page covers the full pattern — --print combined with --output-format and --json-schema — that the exam uses to test practical automation knowledge.

How It's Tested & Common Confusions

Exam questions typically present a CI/CD scenario — a GitHub Actions YAML, a shell script, or a Makefile — and ask which flag or combination of flags makes Claude Code usable in that context. Expect to distinguish --print (non-interactive execution) from --output-format (response structure) and --json-schema (output validation). You may also be asked to identify why a script that omits --print hangs in a headless environment.

Frequently Asked Questions

Is -p exactly the same as --print?

Yes. `-p` is the short form and `--print` is the long form of the same flag. Both put Claude Code into non-interactive mode. The short form is common in shell scripts where brevity matters; the long form is preferable in YAML configuration files where clarity is more important.

Does --print affect which model is used?

No. `--print` only controls the interaction mode. The model is still selected by the `--model` flag or the default configured in your Claude Code settings. Running `claude -p` with the default model runs the same model as an interactive session.

Can I use --print with CLAUDE.md-defined skills?

Yes. You can invoke a skill in non-interactive mode with `claude -p '/skill-name arguments'`. The skill's frontmatter constraints (including `allowed-tools` and `context: fork`) still apply. This is a common pattern for scheduled automation: define a skill with tight tool restrictions, then invoke it headlessly on a schedule.

PrepGenAICerts is an independent exam-prep site. It is not affiliated with, endorsed by, or sponsored by Anthropic, PBC. Claude and Anthropic are trademarks of Anthropic, PBC. Exam details are taken from publicly available Anthropic and Pearson documentation. About PrepGenAICerts.