Automate Repetitive Tasks by Piping Your Shell History to an AI Agent

John Lindquist
InstructorJohn Lindquist

Social Share Links

Tweet

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:

  1. 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.
  2. 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"
    }
    ```

[00:00] Now because I'm getting sick of typing bun build over and over again, let's go ahead and create a plan for that. I'm actually gonna look through my command history. You can use history on most shells. I use a tool called Atuin, which is a history list. And I'm going to type history list and grep for bun build.

[00:17] And I'm going to group that command. So I'll select again. I'm going to group this command with a command of echo in my package.json. Please create a watch script which will trigger a build anytime any of our agent or prompt files change. And then I'm going to pipe the result of these grouped commands into our plan.

[00:41] And once I hit enter, because this inherits from standard in, Our command history with all of the previous commands we ran and then our user prompt, which we just echoed out, will be piped into Claude here and piped into our planner, which will fire off the web searching and go ahead and create a plan for us. It looks like there was just an update a couple seconds ago, I'll go ahead and update after this, but for now I'll just sit back and let this take care of this task I performed over and over again and allow Clawd to figure out a better way for me to do this because it can see the patterns that we established. So anytime you do something like two or three times and you think I need a script for this and that output and the script is in the terminal you can just pull up your history and throw it at Claude and tell you to build something for you. So now we should have a plan built out for us. If I scroll back up, it should be right around here.

[01:34] If we look at this plan and assume everything looks right, if we want to implement this after reading through it, I'll open our terminal again, shut down our current session, I'm going to update Claude code, maybe there's something new and fun in there. Then we can run Clawed and redirect our plans, slash, and grab our script development plan, hit enter, and that will pipe the file in. I'm going to hit shift tab to enable the accept edit modes automatically, and that piped the file in so it has that plan and it's running that plan, and I could fire that entire thing up from the terminal. So our plan did want to create a scripts directory, so we need to allow that. Then we'll go ahead and test the script, allow it to hornswoggle for a bit.

[02:14] Then it will test our watch mode. It verified watch was working. Checking the binaries exist. So now it says it's done. I'll go ahead and exit out.

[02:22] We'll fire off bun watch. So anytime we come into one of our prompts, I'll just add a new line here, hit save. You'll see everything recompiled and I no longer have to build each time, I can just let my tool build for me.