Automate tasks with headless mode
Automate tasks with Gemini CLI. Learn how to use headless mode, pipe data into Gemini CLI, automate workflows with shell scripts, and generate structured JSON output for other applications.
Prerequisites
Section titled “Prerequisites”- Gemini CLI installed and authenticated.
- Familiarity with shell scripting (Bash/Zsh).
Why headless mode?
Section titled “Why headless mode?”Headless mode runs Gemini CLI once and exits. It’s perfect for:
- CI/CD: Analyzing pull requests automatically.
- Batch processing: Summarizing a large number of log files.
- Tool building: Creating your own “AI wrapper” scripts.
How to use headless mode
Section titled “How to use headless mode”Run Gemini CLI in headless mode by providing a prompt as a positional argument. This bypasses the interactive chat interface and prints the response to standard output (stdout).
Run a single command:
gemini "Write a poem about TypeScript"How to pipe input to Gemini CLI
Section titled “How to pipe input to Gemini CLI”Feed data into Gemini using the standard Unix pipe |. Gemini reads the
standard input (stdin) as context and answers your question using standard
output.
Pipe a file:
cat error.log | gemini "Explain why this failed"Pipe a command:
git diff | gemini "Write a commit message for these changes"Use Gemini CLI output in scripts
Section titled “Use Gemini CLI output in scripts”Because Gemini prints to stdout, you can chain it with other tools or save the results to a file.
Scenario: Bulk documentation generator
Section titled “Scenario: Bulk documentation generator”You have a folder of Python scripts and want to generate a README.md for each
one.
-
Save the following code as
generate_docs.sh:#!/bin/bash# Loop through all Python filesfor file in *.py; doecho "Generating docs for $file..."# Ask Gemini CLI to generate the documentation and print it to stdoutgemini "Generate a Markdown documentation summary for @$file. Print theresult to standard output." > "${file%.py}.md"done -
Make the script executable and run it in your directory:
Terminal window chmod +x generate_docs.sh./generate_docs.shThis creates a corresponding Markdown file for every Python file in the folder.
Extract structured JSON data
Section titled “Extract structured JSON data”When writing a script, you often need structured data (JSON) to pass to tools
like jq. To get pure JSON data from the model, combine the
--output-format json flag with jq to parse the response field.
Scenario: Extract and return structured data
Section titled “Scenario: Extract and return structured data”-
Save the following script as
generate_json.sh:#!/bin/bash# Ensure we are in a project rootif [ ! -f "package.json" ]; thenecho "Error: package.json not found."exit 1fi# Extract datagemini --output-format json "Return a raw JSON object with keys 'version' and 'deps' from @package.json" | jq -r '.response' > data.json -
Run
generate_json.sh:Terminal window chmod +x generate_json.sh./generate_json.sh -
Check
data.json. The file should look like this:{"version": "1.0.0","deps": {"react": "^18.2.0"}}
Build your own custom AI tools
Section titled “Build your own custom AI tools”Use headless mode to perform custom, automated AI tasks.
Scenario: Create a “Smart Commit” alias
Section titled “Scenario: Create a “Smart Commit” alias”You can add a function to your shell configuration (like .zshrc or .bashrc)
to create a git commit wrapper that writes the message for you.
-
Open your
.zshrcfile (or.bashrcif you use Bash) in your preferred text editor.Terminal window nano ~/.zshrcNote: If you use VS Code, you can run
code ~/.zshrc. -
Scroll to the very bottom of the file and paste this code:
Terminal window function gcommit() {# Get the diff of staged changesdiff=$(git diff --staged)if [ -z "$diff" ]; thenecho "No staged changes to commit."return 1fi# Ask Gemini to write the messageecho "Generating commit message..."msg=$(echo "$diff" | gemini "Write a concise Conventional Commit message for this diff. Output ONLY the message.")# Commit with the generated messagegit commit -m "$msg"}Save your file and exit.
-
Run this command to make the function available immediately:
Terminal window source ~/.zshrc -
Use your new command:
Terminal window gcommitGemini CLI will analyze your staged changes and commit them with a generated message.
Next steps
Section titled “Next steps”- Explore the Headless mode reference for full JSON schema details.
- Learn about Shell commands to let the agent run scripts instead of just writing them.