Pipe, Redirect, and Automate AI Output in the Terminal

Working with command-line tools is a fundamental skill for any developer. When your CLI tool is an AI, you unlock powerful new ways to automate tasks, but only if you know how to manage its input and output effectively. This lesson demonstrates how to use standard shell features—pipes (|) and redirection (< and >)—to make any command-line AI tool a composable part of your workflow.

Direct Input with Flags

The simplest way to interact with the tool is by passing a string directly using a flag, such as --print or its shorthand -p. The tool processes the string and prints the response directly to the terminal.

claude --print "Hi" # Hello! How can I help you today?

Piping Input from Other Commands

Pipes (|) are used to send the standard output of one command to the standard input of another. This allows you to chain commands together. Here, the output of echo "Sup?" is "piped" into the claude tool.

echo "Sup?" | claude --print # Hey! 👋 # What can I help you with today?

Redirecting Input and Output with Files

Redirection allows you to change where standard input comes from and where standard output goes.

Input Redirection (<)

Instead of typing a prompt, you can write it in a file (e.g., prompt.txt) and use the < operator to feed its contents into the command. This is perfect for long, reusable prompts.

# First, create a prompt file # prompt.txt contains: Please describe the advantages of TypeScript. # Then, redirect the file's content as input claude -p < prompt.txt # TypeScript offers several key advantages: ...

Output Redirection (>)

To save the AI's response, you can redirect its output to a file using the > operator. Instead of printing to the terminal, the response is written directly to the specified file (e.g., typescript.txt). This captures the result for later use.

claude -p < prompt.txt > typescript.txt

This command runs silently, but a new file typescript.txt is created with the AI's full response, allowing you to integrate AI-generated content directly into your projects.

Prompts

Please describe the advantages of TypeScript.

Commands

claude --print "Hi"
claude -p "Bonjour"
echo "Sup?" | claude --print
claude -p < prompt.txt
claude -p < prompt.txt > typescript.txt

Code Snippets

# prompt.txt Please describe the advantages of TypeScript.
Advanced Context Engineering with Claude Code
5 lessons

Advanced Context Engineering with Claude Code

Master “Context Engineering” to give Claude Code precise input for reliable results. Learn pipes, templates, plugins, schemas & more for pro-grade automation.

Share with a coworker

Transcript

[00:00] When you run Clod with the print flag, you can pass in a string, hit enter, then wait for a while, and it will give you a string back. You can also do this with just a dash p. We'll say bonjour, hit enter, and get a string back. Now you can also pipe in any command into Clod. So echo and pipe.

[00:19] So Clod print, which means this string was piped into Clod. And we can also redirect files. Let's say we want to create a prompt.txt file, and we'll say please describe the advantages of TypeScript. Switch back over to our terminal. Then we can clod p and redirect the prompt into clod and then get a string out.

[00:42] So then if we want to capture and save any of these responses we can run the same command then redirect this to typescript.txt so the result of this instead of being printed to the terminal will be passed to this file instead. So you can see we got no response this time but if we open this file we've now captured this text in a meaningful way.