Course contentsShow
← Linux
Lesson 56 of 1,8191. Shell Architecture and Process EnvironmentFree lesson

Capturing Subshell Output Streams

Collect and process stdout and stderr separately from complex subshell command blocks.

In this lesson, you will learn how to isolate and store the standard output and standard error streams from a subshell block into separate variables or files.

Why Separate Streams?

When you run a command block in a subshell using parentheses (...), all actions happen in an isolated environment. Sometimes, a subshell performs both useful calculations and error reporting. If you want to keep your "result" clean while logging "errors" elsewhere, you must direct those streams independently before they leave the subshell.

Managing Streams Independently

To capture these streams separately, we use file descriptor redirection inside the subshell. Standard output is file descriptor 1, and standard error is 2. By redirecting them before the subshell closes, you control exactly where the data flows.

Consider this example where we perform a task that might succeed or fail:

# Capture result into a variable, while sending errors to a log file
result=$( (
    echo "Processing data..."
    ls /nonexistent_folder 2> error.log
) )

echo "Captured output: $result"
echo "Check error.log for issues."

In the code above:

  1. The (...) creates the subshell.
  2. ls /nonexistent_folder generates an error message.
  3. 2> error.log forces that specific stream to a file, preventing it from leaking into the result variable.
  4. The echo "Processing data..." stays on standard output and is successfully stored in result.

Advanced Separation

If you need to capture both into different variables instead of files, you can use process substitution (which you learned previously) or temporary file descriptors. By keeping your output and error streams surgically separated within the subshell, you ensure that your script logic remains robust, preventing error noise from corrupting the data you intend to process further down the line. Always remember that once the subshell closes, any stream that wasn't redirected or captured is lost to the parent environment.

Key Takeaway: Use internal redirection (like 2>) within your subshell parentheses to strip errors from your capture stream, ensuring your parent shell variables contain only the data you intend to process.