Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Process substitution (<() and >()) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Process Substitution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this process substitution command?
Consider the following bash command using process substitution. What will it output?
Bash Scripting
diff <(echo -e "apple\nbanana") <(echo -e "apple\nbanana\ncherry")
A2a3\n> cherry
B1a2\n> banana
CNo output (files are identical)
DSyntax error
Attempts:
2 left
💡 Hint
Process substitution creates temporary files from command outputs for diff to compare.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses process substitution to sort two files and compare them?
You want to compare the sorted contents of file1.txt and file2.txt using diff and process substitution. Which command is correct?
Adiff <(sort < file1.txt) <(sort < file2.txt)
Bdiff >(sort file1.txt) >(sort file2.txt)
Cdiff <(file1.txt | sort) <(file2.txt | sort)
Ddiff <(sort file1.txt) <(sort file2.txt)
Attempts:
2 left
💡 Hint
Process substitution uses <() to provide command output as a file input.
🔧 Debug
advanced
2:00remaining
Why does this process substitution command fail?
You run this command and get an error: 'bash: syntax error near unexpected token `>'': cat >(grep foo > output.txt) Why does it fail?
ABecause >() is for output redirection and cannot be used as an argument to cat
BBecause grep cannot write to output.txt inside process substitution
CBecause cat requires input files, not output redirection
DBecause process substitution only works with <(), not >()
Attempts:
2 left
💡 Hint
Think about what >() does and how cat expects its arguments.
🚀 Application
advanced
2:00remaining
How to merge two sorted lists using process substitution?
You have two sorted files, list1.txt and list2.txt. You want to merge them into one sorted output using the 'sort -m' command and process substitution. Which command achieves this?
Asort -m list1.txt list2.txt
Bsort -m <(cat list1.txt) <(cat list2.txt)
Csort -m >(cat list1.txt) >(cat list2.txt)
Dsort -m <(sort list1.txt) <(sort list2.txt)
Attempts:
2 left
💡 Hint
Process substitution can provide command outputs as files to commands expecting file arguments.
🧠 Conceptual
expert
2:00remaining
What is the main difference between <() and >() in bash process substitution?
Choose the best explanation of the difference between <() and >() in bash process substitution.
A<() redirects output to a file; >() reads input from a file
B<() and >() are interchangeable and both provide command output as input
C<() provides the output of a command as a file input; >() provides a writable file to send data into a command
D<() is used only for input files; >() is used only for output files but not with commands
Attempts:
2 left
💡 Hint
Think about which one creates a readable file and which one creates a writable file.

Practice

(1/5)
1. What does the process substitution syntax <() do in bash scripting?
easy
A. It runs a command and waits for user input.
B. It redirects the output of a command to a file on disk.
C. It creates a temporary file with command output saved.
D. It runs a command and provides its output as a file input to another command.

Solution

  1. Step 1: Understand process substitution <()

    This syntax runs a command and makes its output appear as a file that another command can read.
  2. Step 2: Compare with other options

    Unlike redirecting output to a disk file, <() creates a temporary file descriptor for input.
  3. Final Answer:

    It runs a command and provides its output as a file input to another command. -> Option D
  4. Quick Check:

    Process substitution <() = command output as input file [OK]
Hint: Remember: <() gives command output as a file input [OK]
Common Mistakes:
  • Confusing <() with output redirection >
  • Thinking it creates a real disk file
  • Assuming it waits for user input
2. Which of the following is the correct syntax to use process substitution to compare two files file1 and file2 using diff?
easy
A. diff <(cat file1) <(cat file2)
B. diff >(cat file1) >(cat file2)
C. diff <(file1) <(file2)
D. diff file1 >() file2

Solution

  1. Step 1: Identify correct process substitution usage

    Using <() runs commands and provides their output as files to diff.
  2. Step 2: Check each option

    diff <(cat file1) <(cat file2) correctly uses cat file1 and cat file2 inside <(). Options B, C, and D misuse syntax or operators.
  3. Final Answer:

    diff <(cat file1) <(cat file2) -> Option A
  4. Quick Check:

    Correct syntax for process substitution in diff = diff <(cat file1) <(cat file2) [OK]
Hint: Use <() with commands producing input, not >() [OK]
Common Mistakes:
  • Using >() instead of <() for input
  • Trying to put filenames directly inside <()
  • Mixing redirection symbols incorrectly
3. What will be the output of the following bash command?
comm -12 <(sort fileA.txt) <(sort fileB.txt)

Assuming fileA.txt contains:
apple banana carrot
and fileB.txt contains:
banana carrot date
medium
A. apple banana carrot date
B. banana carrot
C. apple
D. banana carrot date apple

Solution

  1. Step 1: Understand comm -12 with process substitution

    The command compares sorted files and outputs lines common to both (options 1 and 2).
  2. Step 2: Sort and compare file contents

    Sorted fileA.txt: apple, banana, carrot
    Sorted fileB.txt: banana, carrot, date
    Common lines: banana, carrot
  3. Final Answer:

    banana carrot -> Option B
  4. Quick Check:

    Common sorted lines = banana and carrot [OK]
Hint: comm -12 shows common lines from sorted inputs [OK]
Common Mistakes:
  • Not sorting files before comm
  • Expecting all lines combined
  • Confusing comm options
4. Identify the error in this script snippet using process substitution:
grep 'error' >(sort > errors_sorted.txt)
medium
A. Using >() where <() is needed for input
B. Missing quotes around 'error'
C. Cannot redirect output inside >()
D. sort command should be outside process substitution

Solution

  1. Step 1: Analyze the use of >() in the command

    The syntax >() creates a writable file descriptor for output redirection, but here grep expects a file input, not output.
  2. Step 2: Determine correct substitution type

    To provide input from a command, <() should be used. Using >() here is incorrect.
  3. Final Answer:

    Using >() where <() is needed for input -> Option A
  4. Quick Check:

    Input needs <() not >() [OK]
Hint: Use <() for input, >() for output redirection [OK]
Common Mistakes:
  • Confusing input and output process substitution
  • Assuming >() can be nested inside commands
  • Ignoring command expectations for input/output
5. You want to run a command that reads from a file and writes output to another command simultaneously, without creating temporary files on disk. Which process substitution usage correctly achieves this?
command1 <(producer) >(consumer)

What does this setup do?
hard
A. Runs command1 output to producer and input from consumer.
B. Creates two temporary files for producer and consumer.
C. Runs producer and feeds its output as input to command1, then sends command1 output to consumer.
D. Runs consumer first, then producer.

Solution

  1. Step 1: Understand <() and >() roles

    <() provides command output as input file, >() provides a writable file for output redirection.
  2. Step 2: Analyze the command flow

    producer output feeds command1 input via <(), and command1 output is sent to consumer via >().
  3. Final Answer:

    Runs producer and feeds its output as input to command1, then sends command1 output to consumer. -> Option C
  4. Quick Check:

    <() input, >() output redirection [OK]
Hint: Use <() for input, >() for output to chain commands [OK]
Common Mistakes:
  • Mixing input and output substitutions
  • Assuming temporary disk files are created
  • Confusing command execution order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard