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.
command1 <(command2) command1 >(command2)
diff <(ls dir1) <(ls dir2)
tee >(grep 'error' > errors.log) < input.log
cat <(echo 'Hello')sort <(cat file1) <(cat file2)
#!/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
<() do in bash scripting?<()<() creates a temporary file descriptor for input.<() = command output as input file [OK]file1 and file2 using diff?<() runs commands and provides their output as files to diff.cat file1 and cat file2 inside <(). Options B, C, and D misuse syntax or operators.comm -12 <(sort fileA.txt) <(sort fileB.txt)
fileA.txt contains:apple
banana
carrotfileB.txt contains:banana
carrot
datecomm -12 with process substitutionfileA.txt: apple, banana, carrotfileB.txt: banana, carrot, dategrep 'error' >(sort > errors_sorted.txt)
>() creates a writable file descriptor for output redirection, but here grep expects a file input, not output.<() should be used. Using >() here is incorrect.command1 <(producer) >(consumer)
<() and >() roles<() provides command output as input file, >() provides a writable file for output redirection.producer output feeds command1 input via <(), and command1 output is sent to consumer via >().<() input, >() output redirection [OK]Pass the test and this pattern moves unknown