> headless & scripting
Headless mode turns the agent into a Unix citizen: -p runs a single prompt and exits, stdin feeds it data, and --output-format json makes the result parseable. This is how Gemini CLI slots into shell pipelines, git hooks, and CI. The trade-off is that tool use still needs approval, so unattended runs must set an approval mode up front.
// headless & scripting
5 commands$ gemini -p "summarize README.md"$ cat error.log | gemini -p "find the root cause"$ gemini -p "list the exported functions" --output-format json | jq -r '.response'$ gemini -i "explain the architecture of this repo"$ gemini -p "fix the failing test" --approval-mode auto_edit// faq
How do I run Gemini CLI non-interactively in a script?
Use gemini -p "your prompt", which prints the answer to stdout and exits. Pipe input in on stdin (cat file | gemini -p "..."), and redirect stdout to capture the result. Add --output-format json when a script needs to parse the response.
How do I get structured JSON output from Gemini CLI?
Pass --output-format json (or -o json). The result is one JSON object with a response string and a stats block of token and tool usage; parse it with jq, for example jq -r '.response'. On failure the object contains an error field with type, message, and code instead.
Why does my headless prompt hang instead of finishing?
In -p mode the agent still asks for approval before editing files or running commands, and with no TUI there is nothing to confirm it, so it waits forever. Add --approval-mode auto_edit for edits, or --yolo to auto-approve everything, in unattended runs.
Can I keep chatting after an initial scripted prompt?
Yes — use -i (--prompt-interactive) instead of -p. It runs the given prompt as the first turn, then drops you into the interactive session to continue. Use -p when you want the process to exit after one answer.