Lesson
Automate Repetitive Tasks by Piping Your Shell History to an AI Agent
Automate CLI tasks with AI. Pipe shell history into a planner agent to generate, review, and execute plans—turning manual workflows into automation.
- Access
- Included
- Transcript
- Needs source
Stop telling your AI what you want to do and start showing it what you've already done. This lesson unveils a powerful, terminal-native workflow for automating repetitive command-line tasks. You'll learn how to pipe your shell history directly to your "planner" agent, providing it with the rich context of your previous commands. We'll walk through creating a watch script by combining your build history with a simple natural language request, demonstrating the "plan-then-execute" pattern where the AI first generates a plan for your review and then implements it, turning a tedious manual process into an elegant, automated solution.
The Plan-Then-Execute Workflow
This lesson highlights a robust two-step pattern for working with AI:
- Generate a Plan: First, provide the AI with context (your command history and your goal) and ask it to create a detailed, step-by-step plan in a markdown file. This allows you to review the proposed approach before any code is written.
- Execute the Plan: Once you're happy with the plan, you simply feed the generated markdown file back into the AI. The agent will then follow its own instructions to create directories, write code, and update configuration files to implement the solution.
By leveraging standard shell tools like piping (|) and redirection (<), you can orchestrate this entire process without ever leaving your terminal, turning a repetitive annoyance into a powerful, automated workflow.
User Prompts
The user provides a combination of command history and a natural language instruction to the AI agent to generate a plan.
In my package JSON, please create a watch script, which will trigger a build anytime any of our agent or prompt files change.
Terminal Commands
The core of this workflow relies on chaining commands and piping their output to the AI agent.
First, the user constructs a rich context by combining the output of their shell history with a natural language prompt, and pipes it all into the plan command.
(atuin history list | grep "bun build"; echo "In my package JSON, please create a watch script, which will trigger a build anytime any of our agent or prompt files change.") | plan
After an update message appears, the user updates the AI tool.
npm i -g @anthropic-ai/claude-code@latest
To execute the generated plan, the user redirects the plan file as standard input to the claude command.
claude < plans/watch-script-development-plan.md
Finally, the newly created watch script is run.
bun watch
Generated Development Plan Snippet
The AI agent first produces a detailed markdown plan, which outlines the current setup and the proposed implementation.
# Watch Script Development Plan
## Overview
Add a watch script to package.json that automatically rebuilds agent binaries whenever agent or prompt files change.
## Current Setup
- **Agent files**: Located in `agents/` directory (`.ts` files)
- **Prompt files**: Located in `prompts/` directory (`.md` files)
- **Build output**: Compiled binaries in `bin/` directory
- **Build command**: `bun build agents/{agent}.ts --compile --outfile bin/{agent}`
## Implementation Plan
### Option 1: Simple Watch Script with Bun (Recommended)
Create a watch script that uses Bun's built-in watch flag to monitor and rebuild on changes.
**Steps:**
1. Create a `scripts/watch.ts` file that:
- Uses `Bun.Glob` to find all agent files
- Sets up file watchers using `fs.watch`
- Triggers rebuild when files change
- Handles multiple concurrent rebuilds efficiently
2. Add scripts to package.json:
```json
"scripts": {
"build": "bun scripts/build.ts",
"watch": "bun scripts/watch.ts",
"dev": "bun --hot scripts/watch.ts"
}
```