Claude as a command-line tool
claude -p "<prompt>" (short for --print) runs Claude Code without the interactive UI. It does the task, prints the result and exits, which means you can use it in shell scripts, git hooks, cron jobs and CI pipelines.
claude -p "summarize what changed in the last 10 commits"
git log --oneline -20 | claude -p "write release notes from these commits"
cat build-error.txt | claude -p "explain the root cause in two sentences" > diagnosis.txtOutput formats
| Format | Flag | Use |
|---|---|---|
| Text | --output-format text (default) | Humans and simple scripts |
| JSON | --output-format json | Includes result, session_id and total_cost_usd |
| Stream | --output-format stream-json --verbose | Newline-delimited events for live progress |
claude -p "count the TODO comments in src/" --output-format json | jq -r '.result'Need a strict shape? Add --json-schema '<schema>' and read structured_output from the JSON.
Lock it down
Unattended runs should be least-privilege. With no human watching, anything that would normally prompt is denied unless you approve it up front:
claude -p "fix lint errors in src/" \
--allowedTools "Read,Edit,Bash(pnpm lint *)" \
--permission-mode dontAsk \
--max-turns 15--allowedToolspre-approves exactly what's needed, in permission-rule syntax.--permission-mode dontAskdenies everything else.--max-turnsand--max-budget-usdstop runaway jobs.--bareskips auto-discovery of local hooks, skills, plugins, MCP servers and CLAUDE.md for reproducible CI runs (it uses an API key).
1) Is "done" defined? 2) Can it check its own work? 3) What is it allowed to touch? 4) What stops it (turns, budget)? 5) Who reviews the result? If you can't answer all five, keep a human in the loop.
Multi-step scripts
Capture the session ID and continue the same conversation:
sid=$(claude -p "start a review of src/payments" --output-format json | jq -r '.session_id')
claude -p "now check the error handling specifically" --resume "$sid"Running a fix across many files? Try the command on three first, review the diffs, adjust the prompt, then loop over the rest.
Run Claude headless to "list the TODOs in src" with JSON output
Check your understanding
What does claude -p do?
Which output format includes total_cost_usd and session_id?
For a locked-down CI run, which permission mode denies everything not pre-approved?
How do you continue a headless conversation in a second command?