Process Substitution in Shell

Level 488 keystrokes

Process substitution <(command) lets a command's output be used where a file name is expected. Here diff compares two directory listings, and a while loop reads lines from a command.

Shell code

# Each <(ls ...) acts as a file holding that command's output; diff compares them
1diff <(ls dir1) <(ls dir2)
# Read one line per iteration into line
2while read line; do
# Print the line with a prefix
3 echo "Line: $line"
# < <(cmd) feeds the command's output to the loop; unlike a pipe, the loop runs in the current shell
4done < <(cat file.txt)

Output

1c1
< file1.txt
---
> file2.txt
Line: Hello World
Line: Sample text
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • <× 4
  • (× 3
  • )× 3
  • "× 2
  • ;× 1
  • :× 1
  • .× 1
  • $× 1

More Shell examples at level 4

← Shell typing practice