In this categoryGemini ยท 24
- How to Install the Gemini CLI and Run Your First PromptStart
- How to Get a Gemini API Key from Google AI Studio
- How to Call the Gemini API from Python
- How to Give Gemini CLI Project Context with a GEMINI.md File
- How to Choose the Right Gemini Model for Coding Tasks
- How to Fix a Bug in Your Codebase with the Gemini CLI
- How to Set Up Gemini Code Assist in VS Code
- How to Stream Gemini Responses in Node.js
- How to Feed a Long Document into Gemini's Long Context
- How to Run the Gemini CLI Non-Interactively in Scripts
- How to Analyze a Video File with the Gemini API
- How to Connect an MCP Server to the Gemini CLI
- How to Fix Common Gemini CLI Authentication Errors
- How to Cache Long Context in the Gemini API to Cut Costs
- How to Summarize a YouTube Video with Gemini
- How to Analyze PDFs and Docs in the Gemini Chat App
- How to Extract Data from Images and Screenshots with Gemini
- How to Generate a Short Video with Veo in Gemini
- How to Ground Gemini Answers in Your Own Documents
- How to Use Gemini Inside Google Docs to Draft Faster
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
16:12
3:58:44
12:00
09:00
28:14
15:21New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.