Pipelines let one tool feed another. This example compares a unique sorted list with a reverse sorted list.

Program

Play the script to choose the pipeline mode and see the modeled output.

mode
sort_unique_plan.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

mode="unique"
input="beta alpha alpha"
if [ "$mode" = "unique" ]; then
    command="sort | uniq"
    output="alpha beta"
else
    command="sort -r"
    output="beta alpha alpha"
fi
echo "$command -> $output"
#!/usr/bin/env bash

mode="reverse"
input="beta alpha alpha"
if [ "$mode" = "unique" ]; then
    command="sort | uniq"
    output="alpha beta"
else
    command="sort -r"
    output="beta alpha alpha"
fi
echo "$command -> $output"
  1. mode ← unique

    3mode="unique"4input="beta alpha alpha"
    values this stepuniquemode
  2. input ← beta alpha alpha

    3mode="unique"4input="beta alpha alpha"5if [ "$mode" = "unique" ]; then
    values this stepbeta alpha alphainput
  3. if [ "$mode" = "unique" ]; then

    4input="beta alpha alpha"5if [ "$mode" = "unique" ]; then6    command="sort | uniq"
    values this stepuniquemode
  4. command ← sort | uniq

    5if [ "$mode" = "unique" ]; then6    command="sort | uniq"7    output="alpha beta"
    values this stepsort | uniqcommand
  5. output ← alpha beta

    6    command="sort | uniq"7    output="alpha beta"8else
    values this stepalpha betaoutputbeta alpha alphainput
  6. echo "$command -> $output"

    11fi12echo "$command -> $output"
    outputsort | uniq -> alpha beta
    values this stepsort | uniqcommandalpha betaoutput
  1. mode ← reverse

    3mode="reverse"4input="beta alpha alpha"
    values this stepreversemode
  2. input ← beta alpha alpha

    3mode="reverse"4input="beta alpha alpha"5if [ "$mode" = "unique" ]; then
    values this stepbeta alpha alphainput
  3. if [ "$mode" = "unique" ]; then

    4input="beta alpha alpha"5if [ "$mode" = "unique" ]; then6    command="sort | uniq"
    values this stepreversemode
  4. command ← sort -r

    8else9    command="sort -r"10    output="beta alpha alpha"
    values this stepsort -rcommand
  5. output ← beta alpha alpha

    9    command="sort -r"10    output="beta alpha alpha"11fi
    values this stepbeta alpha alphaoutputbeta alpha alphainput
  6. echo "$command -> $output"

    11fi12echo "$command -> $output"
    outputsort -r -> beta alpha alpha
    values this stepsort -rcommandbeta alpha alphaoutput
pipeline A pipeline connects commands so each stage transforms the stream.
sort `sort` orders lines before later processing.
uniq `uniq` removes adjacent duplicate lines after sorting.