In Lesson 1, you learned to create Skills that guide Claude's behavior during interactive conversations. You write instructions in SKILL.md, and Claude follows them when you chat. This works well when you're present to guide the interaction and review results.
However, many tasks need to run without your direct involvement: processing files during meetings, reviewing every pull request automatically, or analyzing thousands of log entries overnight. These scenarios require CLI automation.
CLI automation transforms Claude into a tool that integrates with your workflows. Instead of starting a conversation, you invoke Claude with specific parameters, feed it data through files or standard input, and capture structured output that scripts can process.
This lesson teaches you to invoke Claude from the command line with specific parameters, control accessible tools, structure output for machine parsing, and integrate everything into shell scripts. You'll build three automation scripts: one reviewing changed files in pull requests, one generating API documentation, and one categorizing log errors.
When you invoke Claude in , any you've created remain active. If your instructions match a 's description, automatically applies that guidance. However, you cannot use syntax in — write instructions directly and optionally use for additional context.
The -p flag enables print mode — Claude's non-interactive operation. In print mode, Claude receives instructions, processes input, generates output, prints to stdout, and exits. No conversation, just: input in, output out, process ends.
Basic syntax:
Claude generates an explanation, prints it, and exits. Your terminal prompt returns immediately.
The power emerges when combining with shell features. Capture output in variables, redirect to files, or pipe to other commands. Print mode makes Claude behave like any Unix tool.
When deciding between modes: use interactive for exploration and debugging where you need follow-up questions. Use print mode for batch processing, documentation generation, or automated checks.
The --tools flag accepts a comma-separated list of tool names Claude can access. Specify only what's needed for the task:
Claude can read files (Read) and execute commands (Bash) but cannot edit anything. Available tools: Read, Write, Edit, Bash, Grep, Glob.
The --append-system-prompt flag adds instructions for this specific invocation without modifying Skill files:
The --max-turns flag limits execution iterations. Setting --max-turns 2 allows one response with potential tool use and a final response. Setting --max-turns 1 forces a single response.
Conversational responses return natural language, which is difficult to parse reliably in scripts. The --output-format json flag returns structured data.
When using --output-format json, the CLI wraps the response with metadata (execution time, token usage, costs). Your data lives under .result:
Output structure:
Extract data with jq using .result:
For precise control over output shape, use --json-schema:
The schema guarantees specific fields exist with defined types. With --json-schema, data appears under .structured_output:
Input redirection (<) feeds file contents to Claude:
Pipes (|) connect command output to Claude's input:
Chain multiple commands:
Output redirection (>) writes Claude's response to a file:
Complete example combining all mechanisms:
Create scripts/review-changes.sh:
The script lists staged files, feeds each to Claude for security review, and extracts structured results.
Create scripts/document-apis.sh:
The script finds all API files, extracts documentation using a schema, and formats as Markdown.
Create scripts/analyze-logs.sh:
Filters log errors, categorizes them, and outputs structured results.
The dot notation .result or .structured_output accesses fields in JSON objects depending on which flag you used. Square brackets [] iterate over arrays, so .result.categories[] processes each category element.
The jq pipe operator chains operations. The expression .result.categories[] | select(.severity == "high") gets all categories, then keeps only high-severity ones.
For most automation, you'll extract fields using .result.field_name with --output-format json or .structured_output.field_name with --json-schema.
You've learned to invoke Claude from the command line using print mode (-p), restrict operations with --tools, add context with --append-system-prompt, and limit iterations with --max-turns. For output, use --output-format json for structured data in .result or --json-schema for precise shapes in .structured_output. Shell mechanisms like input redirection, pipes, and output redirection connect Claude with other commands.
You have a complete toolkit: Skills defining reusable behaviors, Plugins packaging them for sharing, and CLI automation integrating everything into workflows. Skills work automatically in both interactive mode and automation.
