typestar

Process substitution in Bash

A command's output, standing in for a file.

# <( ) lets a command's output stand in for a file
diff <(printf 'a\nb\nc\n') <(printf 'a\nx\nc\n') || true

# comm wants sorted files; process substitution sorts on the fly
comm -12 <(printf 'b\na\n' | sort) <(printf 'c\nb\n' | sort)

# tee splits a stream: one copy on, one copy saved
tmp=$(mktemp)
seq 1 3 | tee "$tmp" | wc -l
rm "$tmp"

How it works

  1. <( ) gives commands like diff a file-shaped stream.
  2. comm gets its sorted inputs made to order.
  3. tee copies a stream to a file while it keeps flowing.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
339
Tokens
52
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 42 seconds.

Type this snippet

Step 3 of 3 in Pipes & redirection, step 18 of 26 in Language basics.

← Previous Next →