Web Analytics

Process Substitution

Intermediate~20 min read

Process substitution lets you pass the output of a command where a filename is expected, and send data into a process without temp files.

Reading with <()

Output
Click Run to execute your code
Note: This feature requires Bash (and /proc or named pipes). It may not work in strictly POSIX shells.
Pro Tip: Use with diff, comm, paste, and any tool expecting file paths.
Caution: Some tools might fully read inputs before processing, which can affect memory usage.

Common Mistakes

1) Using in POSIX sh

# Wrong
/bin/sh -c 'diff <(echo a) <(echo b)'
# Correct
bash -c 'diff <(echo a) <(echo b)'

2) Forgetting quotes around paths

# Always quote variables holding paths
diff "$(some_cmd_generating_path)" file

Exercise: Compare Sorted Lists

Task: Create two lists in variables and show a diff of their sorted forms using process substitution.

Output
Click Run to execute your code
Show Solution
a=$'c\na\nb'
b=$'b\nc\nd'
diff <(printf '%s\n' "$a" | sort) <(printf '%s\n' "$b" | sort)

Summary

  • Use <() to pass command output where a file is needed.
  • Use >() to feed a process like it was a file.
  • Avoid temp files and simplify complex chains.

What's Next?

Move on to file handling in File Reading.