MODULE 03

CLI WORKFLOWS

Chain commands together, pipe data between tools, and automate repetitive tasks with shell scripts.

Learning objectives

  • > Understand Unix pipes and how to chain commands
  • > Use Claude Code in non-interactive mode for scripting
  • > Build shell scripts that automate multi-step workflows
  • > Create a practical content pipeline for journalism work

Before you start

This module is about building automated pipelines — scripts that run a sequence of AI tasks without you manually intervening. To use them well, it helps to understand what's actually happening inside an AI session: how context is stored, why order matters, and what "caching" means for how quickly your pipeline runs.

Read the full explanation: how AI session memory works →

Key concepts

Pipes and redirection

The pipe character (|) sends the output of one command as input to another. This lets you build processing chains:

cat article.txt | wc -w    # Count words in article
ls *.txt | wc -l           # Count text files

Redirection (> and >>) sends output to files instead of the screen. > overwrites, >> appends.

Non-interactive mode

Claude Code can run as a single command without entering the interactive REPL. This is essential for scripting:

# Interactive (starts a session)
claude

# Non-interactive (runs once and exits)
claude -p "Summarize this article" < article.txt

The -p flag specifies the prompt. Input can come from a file or piped from another command.

Shell scripts

A shell script is a text file containing commands that run in sequence. Scripts let you save and reuse workflows. On macOS and Linux, scripts typically start with #!/bin/bash and need execute permission (chmod +x script.sh).

Variables and loops

Shell scripts can use variables and loops to process multiple files:

for file in *.txt; do
  echo "Processing $file"
  claude -p "Summarize" < "$file" > "${file%.txt}-summary.txt"
done

This loop processes every .txt file and creates a summary for each.

EXERCISE

GOAL: Ask Claude to build a newsletter pipeline that uses claude -p to process articles automatically — then explore what it built and why it works.

01 Open Claude Code

Open your terminal and launch a session:

terminal
claude

02 Ask Claude to build the pipeline

Paste this prompt inside the session:

claude code
Create a newsletter pipeline in ~/newsletter-pipeline. I need:

1. Two realistic sample news articles as .txt files (fictional local news stories)
2. A shell script called process-newsletter.sh that uses "claude -p" (non-interactive mode) to process each .txt file: summarize it in 2-3 sentences, then format that summary as a simple HTML snippet with an h3 headline and paragraph. Save outputs to an output/ folder.
3. Make the script executable, run it on one article, and show me the output.

Watch what Claude does: it creates the folder structure, writes the sample articles, writes the script, and runs it. You delegated the whole thing.

03 Ask Claude to explain what it built

In the same session, ask Claude to explain the key concept:

claude code
Walk me through the script line by line. Explain what "claude -p" does and how it's different from the interactive session we're using right now. What would happen if I added a third article to the folder and ran the script again?

This is the key concept of the module: claude -p is non-interactive mode — it runs once and exits, making it scriptable. Your interactive session is the opposite: persistent, conversational, context-aware.

04 Process all articles

Ask Claude to run the pipeline on both articles and show you the results:

claude code
Run the pipeline on all the articles and show me each HTML output. Then tell me: what would need to change in the script if I wanted to use Gemini CLI instead of Claude?

The script Claude wrote is yours — save it, modify it, run it on real articles whenever you need. That's what "reusable workflow" means.

Checkpoint

Self-check: Make sure you can answer these before moving on.

  • ? What is claude -p and when would you use it instead of an interactive session?
  • ? What is the difference between the interactive mode you used in this exercise and the scripted mode the pipeline uses?
  • ? What kinds of journalism workflows are well-suited to an automated pipeline like this one?
  • ? Why does the script save outputs to files rather than just printing them to the terminal?

Resources

  • [DOCS] Bash Reference Manual - Complete bash documentation
  • [COURSE] The Missing Semester: Shell Tools - MIT's guide to shell scripting
  • [REF] Claude Code documentation - Non-interactive mode reference
  • [TOOL] ExplainShell - Paste any shell command to see what each part does

Troubleshooting

"Permission denied" when running the script

You need to make the script executable: chmod +x newsletter-pipeline.sh

"command not found: claude"

Claude Code isn't in your PATH. If you used the native installer (curl -fsSL https://claude.ai/install.sh | sh), restart your terminal. See code.claude.com/docs for troubleshooting steps.

Script produces empty output

Check that the input file exists and isn't empty. Add set -e at the top of your script to stop on errors, making problems easier to diagnose.

Special characters in filenames cause errors

Always quote variable references in scripts: use "$file" not $file. This handles spaces and special characters correctly.