Automation & Cloud Execution/Local Automation & CI/CD
★ OverviewIntermediate12 min

Pipe Mode — Claude Code as a Unix Citizen

The -p flag turns Claude Code into a composable Unix utility: feed it stdin, get structured output, and chain it with grep, jq, and awk. This article covers every output format, structured output with JSON Schema, resource limits, and real automation patterns.

Quick Reference

  • -p flag: non-interactive mode — Claude responds once and exits with code 0
  • Piped input: cat file | claude -p "prompt" — stdin becomes part of the context
  • --output-format json returns a single JSON object; stream-json streams tokens
  • --json-schema <file> validates output against a schema you provide
  • --max-turns and --max-budget-usd cap runaway loops and costs
  • claude -c -p continues the previous session non-interactively
  • Exit code 0 = success, non-zero = error — scriptable like any Unix tool
  • Pipe output into jq, grep, awk, or another claude -p call

What Pipe Mode Is

Claude Code is designed first as an interactive pair-programmer, but the -p flag turns it into a composable command-line utility. With -p, Claude reads stdin (if any), runs one turn, prints the result to stdout, and exits. No interactive session, no TUI, no permission prompts — just input in, output out.

Why -p Matters for Automation

Non-interactive mode lets you embed Claude Code in shell scripts, Makefiles, CI pipelines, cron jobs, and any other tool that expects a normal Unix command. The model becomes a function: call it with arguments, get a deterministic-ish response.

All three are single-turn, non-interactive invocations

When stdin is a terminal (no piped input), -p simply uses the prompt as the sole user message. When stdin is a pipe, the piped content is prepended to the context. This makes it trivially easy to send large files or command output to Claude without manually copying them.