Challenge - 5 Problems
Stderr Redirection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this command?
Consider the command:
What will be the output of
ls /nonexistent 2> error.log; cat error.log
What will be the output of
cat error.log?Linux CLI
ls /nonexistent 2> error.log; cat error.logAttempts:
2 left
💡 Hint
The 2> operator redirects error messages to a file.
✗ Incorrect
The command tries to list a non-existing directory. The error message is redirected to 'error.log'. Using 'cat error.log' shows the error message inside the file.
💻 Command Output
intermediate2:00remaining
What happens when appending stderr with 2>>?
Given the commands:
What will be the output of
echo 'First error' 1>&2 2> error.log echo 'Second error' 1>&2 2>> error.log cat error.log
What will be the output of
cat error.log?Linux CLI
echo 'First error' 1>&2 2> error.log echo 'Second error' 1>&2 2>> error.log cat error.log
Attempts:
2 left
💡 Hint
2> overwrites, 2>> appends to the file.
✗ Incorrect
The first echo sends 'First error' to stderr, redirected to error.log (overwrites). The second echo appends 'Second error' to error.log. So both lines appear in the file.
🔧 Debug
advanced2:00remaining
Why does this command not redirect stderr as expected?
You run:
But errors.log remains empty even if file.txt does not exist. Why?
grep 'pattern' file.txt | wc -l 2> errors.log
But errors.log remains empty even if file.txt does not exist. Why?
Linux CLI
grep 'pattern' file.txt | wc -l 2> errors.log
Attempts:
2 left
💡 Hint
Think about which command's stderr is redirected.
✗ Incorrect
In shell pipelines, a redirection like 2> after a pipe applies to the preceding command (wc -l here), not the first command (grep). Grep's stderr goes to the terminal, and wc -l produces no stderr, leaving errors.log empty.
📝 Syntax
advanced2:00remaining
Which command correctly appends stderr to a file?
Choose the command that appends stderr output to errors.log without overwriting it.
Attempts:
2 left
💡 Hint
Appending means adding to the end of the file.
✗ Incorrect
2>> appends stderr to the file. 2> overwrites. The other options are invalid syntax.
🚀 Application
expert3:00remaining
How to redirect both stdout and stderr to different files simultaneously?
You want to run
mycommand and save its standard output to out.log and its error output to err.log. Which command does this correctly?Attempts:
2 left
💡 Hint
Order of redirections matters less here, but appending vs overwriting does.
✗ Incorrect
Both A and B redirect stdout and stderr to separate files correctly. However, the order does not affect the result here. Options C and D append stderr instead of overwriting. The question asks for saving outputs, not appending. So A is the best answer.