A pipeline sends stdout from one command into stdin of the next. Each command does one small job.

Program

Play the script to see generated log text flow into grep.

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

log=$'error\nok\nerror'
matches="$(printf '%s\n' "$log" | grep -c error)"
echo "matches=$matches"
  1. log ← error, ok, error

    3log=$'error\nok\nerror'4matches="$(printf '%s\n' "$log" | grep -c error)"
    values this steperror, ok, errorlog
  2. matches ← 2

    3log=$'error\nok\nerror'4matches="$(printf '%s\n' "$log" | grep -c error)"5echo "matches=$matches"
    values this step2matcheserror, ok, errorlog
  3. echo "matches=$matches"

    4matches="$(printf '%s\n' "$log" | grep -c error)"5echo "matches=$matches"
    outputmatches=2
    values this step2matches

Watch stdout Flow

  1. printf writes three log lines to stdout.
  2. The pipe sends those lines into grep.
  3. grep -c error counts matching lines.
  4. Command substitution stores the count in matches.
printf stdout -> grep stdin -> count of "error"

| Line | Counted by grep -c error? | | --- | --- | | error | yes | | ok | no | | error | yes |

pipeline `cmd1 | cmd2` connects the first command's stdout to the second command's stdin.
filter A filter reads input, selects or transforms it, and writes a result.

Exercise: pipelines.sh

Pipe generated log lines into grep -c and print how many error lines matched