Lesson

Pipe, Redirect, and Automate AI Output in the Terminal

Transform Claude Code CLI into a flexible tool using pipes (|) and redirection (<, >). Feed input from files or commands and save output automatically.

Access
Included
Transcript
Needs source

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.