0
0
Bash Scriptingscripting~10 mins

Process substitution (<() and >()) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Process substitution (<() and >())
Start command needing file input/output
Use <() or >() to create temp FIFO or /dev/fd
Run inner command inside <() or >()
Shell connects FIFO or file descriptor to outer command
Outer command reads from or writes to this stream
Process substitution ends when commands finish
Process substitution lets a command use the output or input of another command as if it were a file, using special temporary streams.
Execution Sample
Bash Scripting
diff <(echo "apple") <(echo "orange")
Compares outputs of two echo commands as if they were files using process substitution.
Execution Table
StepActionCommand/SubshellFile Descriptor/FIFO CreatedOuter Command InputOutput Produced
1Start diff commanddiffNone yetWaiting for inputsNone
2Create process substitution for first <()echo "apple"/dev/fd/63 (or FIFO)diff reads from /dev/fd/63Outputs 'apple'
3Create process substitution for second <()echo "orange"/dev/fd/62 (or FIFO)diff reads from /dev/fd/62Outputs 'orange'
4diff compares inputsdiffReads from both descriptorsReads 'apple' and 'orange'Outputs diff lines
5diff outputs resultdiffNo new descriptorsN/A1c1 < apple --- > orange
6Commands finishAllDescriptors closedNo inputProcess substitution ends
💡 Process substitution ends after both inner commands finish and diff completes comparison.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
/dev/fd descriptorsNone/dev/fd/63 created for first echo/dev/fd/62 created for second echoBoth descriptors open for diffDescriptors closed
diff input streamsNoneReads from /dev/fd/63Reads from /dev/fd/62Reads both streamsNo input
diff outputNoneNoneNoneDiff result linesOutput complete
Key Moments - 3 Insights
Why does diff treat <(echo "apple") like a file?
Because process substitution creates a temporary file descriptor or FIFO that looks like a file, so diff can read from it as if it were a normal file (see execution_table steps 2 and 3).
What happens if the inner command in <() runs slowly?
The outer command (diff) waits for input from the file descriptor until the inner command produces output or finishes, so the process substitution streams data as it becomes available (see execution_table step 4).
Can process substitution be used for output redirection with >()?
Yes, >() creates a writable file descriptor that the outer command writes to, and the inner command reads from that stream (not shown here but works similarly to <()).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is created for the second process substitution?
A/dev/fd/62 or a FIFO
B/dev/fd/63 or a FIFO
CA regular file on disk
DNo file descriptor is created
💡 Hint
Check the 'File Descriptor/FIFO Created' column at step 3 in the execution_table.
At which step does diff start reading both inputs simultaneously?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for when diff reads from both descriptors in the 'Outer Command Input' column.
If the first echo command output changed to "banana", what would change in the execution_table output?
AThe process substitution would fail
BThe file descriptor numbers would change
CThe diff output lines would show 'banana' instead of 'apple'
DNo change in output
💡 Hint
Check the 'Output Produced' column at step 5 and consider what diff compares.
Concept Snapshot
Process substitution lets commands use outputs or inputs of other commands as files.
Syntax: <(command) for input, >(command) for output.
Shell creates temporary file descriptors or FIFOs.
Outer command reads/writes these like files.
Useful for commands needing file arguments but you want to use command output directly.
Full Transcript
Process substitution in bash scripting allows a command to use the output or input of another command as if it were a file. When you write diff <(echo "apple") <(echo "orange"), the shell runs the echo commands inside temporary file descriptors or FIFOs. These act like files for diff to read. The shell connects these streams so diff can compare the outputs. The process substitution ends when all commands finish. This lets you avoid creating real temporary files and makes scripts cleaner and faster.