No history yet

Advanced Command Chaining

Pipes and Redirection

Pipes (|) are a cornerstone of shell scripting, enabling you to chain commands by redirecting the standard output of one command to the standard input of another. This creates powerful, linear workflows directly in your terminal. For instance, you can use Claude Code to list files in your project context and then pipe that list to grep to filter for specific file types.

# List files in context and find all Markdown files
claude ls -c | grep '\.md$'

Similarly, you can use redirection to capture Claude Code's output. The > operator redirects output to a file, overwriting its contents, while >> appends the output. This is useful for logging, creating documentation, or saving generated code snippets.

# Ask Claude to generate a function and save it to a new file
claude "Generate a Python function to calculate Fibonacci numbers" > fibonacci.py

# Append a commit message suggestion to a log file
claude "Suggest a git commit message for the recent changes" >> commit_logs.txt

Combine commands with shell pipes for powerful workflows

Logical Operators for Control Flow

Logical operators provide conditional execution, allowing you to build more robust command chains that react to the success or failure of previous commands. They are essential for scripting automated tasks that require error handling or sequential validation.

The AND operator (&&) executes the subsequent command only if the preceding command succeeds (returns an exit code of 0).

# Run tests, and only if they pass, ask Claude to commit the changes
claude /run_tests && claude /commit -m "Refactor feature X, all tests passing."

Conversely, the OR operator (||) executes the next command only if the preceding one fails (returns a non-zero exit code). This is perfect for simple error handling, like logging a failure or attempting a fallback operation.

# Attempt to apply a patch, and if it fails, ask Claude to analyze the conflict
claude /apply_patch feature-update.patch || claude "The patch failed to apply. Analyze git status and suggest a resolution."

The semicolon (;) is a simpler operator that executes commands sequentially, regardless of their success or failure. It's useful for grouping related but independent operations.

Isolating Operations with Subshells

Subshells, denoted by parentheses (), execute a group of commands in a separate shell process. This is incredibly useful for isolating operations, preventing them from affecting your current shell's environment. For example, you can temporarily change directories, set environment variables, or perform a series of setup tasks without leaving a trace in your parent shell.

# Clone a repo into a temp directory, have Claude analyze it, then clean up
# The parent shell's current directory is unaffected.
( \
  mkdir temp_analysis && \
  cd temp_analysis && \
  git clone https://github.com/example/project.git && \
  claude -c project "Summarize the architecture of this project." > ../summary.txt && \
  cd .. && \
  rm -rf temp_analysis \
)

echo "Analysis complete. Summary saved to summary.txt"

Building Reusable Workflows

To maximize efficiency, you can encapsulate complex command chains into reusable aliases or shell functions. This turns a long, multi-step command into a short, memorable one.

An alias is a simple substitution. A shell function allows for more complexity, including arguments and internal logic.

# Create a shell alias for a common Claude Code task
alias claude_review='claude "Review the staged changes for code quality and suggest improvements."' 

# Create a shell function to test and commit with a Claude-generated message
claude-tc() {
  claude /run_tests && \
  claude "Generate a concise, conventional commit message for the staged changes." | xargs -I {} git commit -m "{}"
}

After defining this function in your .bashrc or .zshrc, you can simply run claude-tc in your terminal to execute the entire workflow. This is a powerful way to automate your personal development process.

Quiz Questions 1/6

What is the primary function of the pipe operator (|) in a shell command?

Quiz Questions 2/6

You want to add the output of the date command to the end of an existing log file named activity.log without deleting its current contents. Which command should you use?