How to Run the Gemini CLI Non-Interactively in Scripts
Pass a prompt with the -p flag and pipe input so the Gemini CLI works inside shell pipelines and CI.
The Gemini CLI is interactive by default, but it also runs as a one-shot command. That lets you drop it into shell scripts, git hooks, and CI jobs. This guide covers passing prompts directly, piping in file contents, and capturing the output.
What you need
- The Gemini CLI installed and authenticated
- A terminal comfortable with pipes and redirection
- Optional: a Gemini API key for use in CI
- About 5 minutes
Step 1: Pass a prompt with -p
The -p (or --prompt) flag runs a single prompt and prints the answer to stdout without opening the interactive UI. This is the building block for everything else.
Step 2: Pipe input into a prompt
You can pipe a file or command output into the CLI and reference it in your prompt. This is the fastest way to ask about a specific log, diff, or file.
git diff | gemini -p "Write a concise commit message for this diff."
cat error.log | gemini -p "What is the root cause of this stack trace?"Step 3: Capture output in a script
Because output goes to stdout, you can capture it in a variable or redirect it to a file. The snippet below builds a commit message from staged changes inside a shell script.
#!/usr/bin/env bash
set -euo pipefail
msg=$(git diff --cached | gemini -p "Write a one-line conventional commit message.")
echo "Suggested: $msg"
git commit -m "$msg"Result
The Gemini CLI now behaves like any other Unix tool: it reads stdin, writes stdout, and slots into pipelines. You can chain it with git, grep, and jq, or call it from CI to automate review notes and changelogs.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31