0
0
Bash Scriptingscripting~5 mins

Process substitution (<() and >()) in Bash Scripting

Choose your learning style9 modes available
Introduction
Process substitution lets you use the output or input of a command as if it were a file. This helps connect commands easily without creating temporary files.
You want to compare outputs of two commands using diff without saving files.
You want to feed the output of a command as input to another command that expects a file.
You want to save the output of a command into another command that reads from a file.
You want to avoid creating temporary files when chaining commands.
You want to use commands that require file arguments but want to use dynamic data.
Syntax
Bash Scripting
command1 <(command2)
command1 >(command2)
The <() syntax runs command2 and provides its output as a file-like input to command1.
The >() syntax runs command2 and provides a file-like output to command1, so command1 writes to it.
Examples
Compare the list of files in two directories without creating temporary files.
Bash Scripting
diff <(ls dir1) <(ls dir2)
Send input.log content to tee, which writes to both standard output and to the grep command that filters 'error' lines into errors.log.
Bash Scripting
tee >(grep 'error' > errors.log) < input.log
Print 'Hello' by using process substitution as a file input to cat.
Bash Scripting
cat <(echo 'Hello')
Sort combined contents of file1 and file2 without creating a merged file.
Bash Scripting
sort <(cat file1) <(cat file2)
Sample Program
This script creates two directories with different files, lists their contents, then uses process substitution with diff to compare the file lists without creating temporary files. It prints the differences found.
Bash Scripting
#!/bin/bash

# List files in two directories and compare them
mkdir -p dir1 dir2

echo 'fileA' > dir1/fileA.txt
echo 'fileB' > dir1/fileB.txt

echo 'fileA' > dir2/fileA.txt
echo 'fileC' > dir2/fileC.txt

# Show files in dir1
ls dir1

# Show files in dir2
ls dir2

# Use process substitution to compare files

diff_output=$(diff <(ls dir1) <(ls dir2))

# Print diff result
if [ -z "$diff_output" ]; then
  echo "Directories have the same files."
else
  echo "Differences found:"
  echo "$diff_output"
fi

# Clean up
tmp_dirs=(dir1 dir2)
for d in "${tmp_dirs[@]}"; do
  rm -r "$d"
done
OutputSuccess
Important Notes
Process substitution uses named pipes or /dev/fd files internally, so it does not create real temporary files on disk.
Time complexity depends on the commands used inside the substitution, not on the substitution itself.
A common mistake is to forget that process substitution provides a filename, so you must use commands that accept filenames as arguments.
Use process substitution when you want to avoid temporary files and your commands support file arguments.
Summary
Process substitution lets you treat command output or input as files.
Use <() to provide command output as input file to another command.
Use >() to provide a writable file to a command that sends output to another command.