Home Bash Pipe |
Post
Cancel

Bash Pipe |

Basic Usage

  • Piping Between Commands:
    1
    
    command1 | command2
    

    Output of command1 is used as input for command2.

Combining Multiple Commands

  • Sequential Commands:
    1
    
    command1 | command2 | command3
    

    Output of command1 is passed to command2, and its output is passed to command3.

Filtering and Processing Text

  • Grep:
    1
    
    command | grep 'pattern'
    

    Filters the output of command for lines containing ‘pattern’.

  • Sort:
    1
    
    command | sort
    

    Sorts the output of command.

  • Awk:
    1
    
    command | awk '{print $1}'
    

    Processes the output of command using awk (e.g., prints the first column).

  • Sed:
    1
    
    command | sed 's/old/new/'
    

    Processes the output of command using sed (e.g., replaces ‘old’ with ‘new’).

Redirection and File Operations

  • Tee:
    1
    
    command | tee file.txt
    

    The output of command is both displayed and written to file.txt.

Combining Text Streams

  • Paste:
    1
    
    command1 | paste - command2
    

    Combines the output of command1 and command2 side by side.

Networking and Data Transfer

  • Curl and Processing:
    1
    
    curl -s http://example.com | grep 'title'
    

    Fetches data from a URL and filters it.

  • SSH and Remote Commands:
    1
    
    ssh user@host 'cat file' | grep 'pattern'
    

    Executes a command on a remote machine and processes the output locally.

Advanced Usage

  • Process Substitution:
    1
    
    diff <(command1) <(command2)
    

    Compares the output of two commands using diff.

  • Xargs for Batch Processing:
    1
    
    command1 | xargs -n1 command2
    

    Uses the output of command1 as arguments for command2, one at a time.

Debugging and Analysis

  • Pipe Viewer (pv):
    1
    
    command1 | pv | command2
    

    Monitors the progress of data through a pipe between two commands.

Chains with Conditional Execution

  • AND and OR Lists:
    1
    2
    
    command1 && command2 | command3
    command1 || command2 | command3
    

    Executes command2 only if command1 succeeds/fails, then pipes to command3.

This post is licensed under CC BY 4.0 by the author.