Extract lines containing ERROR from both standard output and standard error of run-check.
Pass the two streams to grep through one Bash pipeline.
Which command is correct?
A normal pipe connects only standard output. Redirecting 2>&1 before the pipe merges standard error into the stream that grep receives.
Detailed explanation
run-check 2>&1 | grep ERRORCorrect. Both streams are merged before the pipeline sends them to grep.
Correct. Both streams are merged before the pipeline sends them to grep.
run-check | grep ERROR 2>&1Incorrect. The 2>&1 applies to grep's descriptors, not run-check's error stream.
Incorrect. The 2>&1 applies to grep's descriptors, not run-check's error stream.
run-check 2> grep ERRORIncorrect. This redirects run-check's error into a file named grep rather than creating a pipeline.
Incorrect. This redirects run-check's error into a file named grep rather than creating a pipeline.
run-check > grep ERRORIncorrect. It redirects output to a filename rather than invoking grep as a filter.
Incorrect. It redirects output to a filename rather than invoking grep as a filter.
Try it yourself
An example you can run in a temporary verification environment.
bash -c 'printf "INFO ok\n"; printf "ERROR bad\n" >&2' 2>&1 | grep ERRORExpected result
ERROR badKey points
- A pipe connects standard output
- 2>&1 merges standard error
- Merge before the pipe
Notes
- Environment: Bash 5.2 / GNU grep 3.x
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.