GeminiCLI.net

The Ultimate Automation Guide: Using Gemini CLI in Your Scripts

By Gemini Guides on 6/16/2025

One of the best, yet most underutilized, features of any command-line interface (CLI) tool is its ability to be scripted. You can combine it with other commands and embed it in simple scripts to automate away the boring, repetitive parts of your day.

This guide will walk you through creating a few practical shell scripts that leverage Gemini CLI to save you time and effort.

Pre-requisite: Make Your Scripts Executable

For any script you create, you'll need to make it executable by running the chmod +x your_script_name.sh command. It's also recommended to place your scripts in a directory that's included in your system's PATH (like /usr/local/bin) so you can call them from anywhere.

Video Primer: Understanding Shell Scripts

If you're new to shell scripting, this excellent crash course explains the fundamental concepts of creating and running scripts, including the chmod +x command.

1. The "Summarize This" Script

Ever get sent a link to a long article or a dense piece of documentation and just need the key takeaways? This script uses Gemini's web search capability to summarize any URL you give it.

The Script (summarize.sh):

#!/bin/bash
 
# Check if a URL was provided
if [ -z "$1" ]; then
  echo "Usage: summarize <URL>"
  exit 1
fi
 
# Call Gemini CLI with the search flag to summarize the URL
gemini --search "$1" "Provide a concise summary of the content at this URL in three key bullet points."

How to Use It:

summarize https://blog.google/technology/developers/introducing-gemini-cli-open-source-ai-agent/

2. The "Refactor This Code" Script

This script is a lifesaver for developers. It takes a file as input, sends its content to Gemini, and asks for a refactored, improved version of the code, printing the result directly to the console.

The Script (refactor.sh):

#!/bin/bash
 
# Check if a file was provided
if [ -z "$1" ]; then
  echo "Usage: refactor <file_path>"
  exit 1
fi
 
# Call Gemini CLI with the file flag and a prompt to refactor it
gemini -f "$1" "Refactor this code to be more efficient, readable, and adhere to modern best practices. Only output the raw code, with no commentary."

How to Use It:

refactor src/components/old-component.js

Pro-Tip: You can even pipe the output directly into a new file: refactor old.js > new.js.

3. The "Generate an Image Idea" Script

Stuck in a creative rut? This fun script uses Gemini to brainstorm a creative prompt for an AI image generator like Midjourney or DALL-E.

The Script (idea.sh):

#!/bin/bash
 
# A simple prompt to get the creative juices flowing
PROMPT="Generate a creative, detailed, and imaginative prompt for an AI image generator. The theme is: Cyberpunk-fantasy landscape."
 
# Call Gemini
gemini "$PROMPT"

How to Use It:

Just run the script and get your inspiration!

idea

Conclusion

These are just simple examples, but they illustrate a powerful concept: if you can describe a repetitive task in words, you can probably automate it with a Gemini CLI script. Think about the commands you type most often and see if you can wrap them in a simple script. It's a small investment of time that pays huge dividends in productivity.

Share this article: