How to use process substitution in bash

· Category: Linux

Short answer

Process substitution <(...) and >(...) lets you use command output or input as if it were a file.

Steps

  1. Compare command outputs:
diff <(sort file1.txt) <(sort file2.txt)
  1. Pass command output as a file argument:
wc -l <(find /var/log -name "*.log")
  1. Write to a process:
echo "data" >(cat > output.txt)

Tips

  • Process substitution creates named pipes behind the scenes.
  • More convenient than creating temporary files manually.
  • Works in bash and zsh; not in plain POSIX sh.

Common issues

  • Some commands require seekable files and do not work with pipes.
  • Nested process substitution can be hard to read; use sparingly.