Pipe and Redirection

Shell Foundation Concepts

Pipe and Redirection

In shell scripting, pipes and redirection are fundamental constructs that enable powerful command chaining and input/output manipulation. These concepts form the backbone of shell scripting, allowing users to process data dynamically and control command flows efficiently.


Pipe Concepts

A pipe connects the output of one command to the input of another, enabling sequential processing. The syntax command1 | command2 directs the output of command1 to command2 as its input.

Key Features

  • Flow Control: Pipes allow commands to be linked in a logical sequence, such as filtering data with grep and counting lines with wc.
  • Flexibility: Pipes can combine commands with any combination of operators (e.g., sort | uniq | wc).
  • Error Handling: Pipes can capture errors using 2>&1 to redirect stderr to stdout.

Example

grep "error" log.txt | wc -l

This command filters lines containing "error" in log.txt and counts the number of such lines.


Redirection Concepts

Redirection allows users to control the input or output of commands, enabling advanced data manipulation.

Input Redirection

  • < filename: Redirects the input of a command from a file.
  • < pipe: Passes the output of another command as input.

Output Redirection

  • > filename: Redirects the output of a command to a file.
  • > filename 2>&1: Redirects both standard output and error to a file.

Example

ls -l > output.txt

This command lists directory contents and saves the output to output.txt.


Combined Use of Pipe and Redirection

Combining pipes and redirection enables complex workflows. For instance:

  • Redirecting input to a pipe:

    cat input.txt | grep "keyword" > result.txt
    

    This reads input.txt, filters lines with "keyword", and saves the output to result.txt.

  • Redirecting output to a file via a pipe:

    command1 | tee output.log > final.txt
    

    This captures the output of command1 and saves it to output.log, while final.txt is a secondary output.


Best Practices

  • Use 2>&1 to capture errors in pipelines.
  • Avoid unnecessary redirections to prevent performance overhead.
  • Test pipelines with bash -x for debugging.

By mastering pipe and redirection, users can create efficient, dynamic shell scripts that handle data with precision and flexibility.