The Warnet Desa’s work desks: stdout and stderr are two chutes. > 2> 2>&1 /dev/null | grep run in a model bash; watch each line land on the screen, in a file…
"I redirected it to a file but the error still shows", "grep does not catch my error", "my log emptied itself": three of the most common shell puzzles, and all three are about two streams and the order of redirections.
> moves stdout only, and bash reads redirections from the left
cmd > file but the error still shows on screen > is stdout only. The error is stderr: add 2> errors.txt, or 2>&1 after > file.2>&1 but the error still does not go into the file The order is backwards. > f 2>&1 is right (both to f); 2>&1 > f copies fd 1’s target *at that moment*, the screen, before fd 1 moves.cmd | grep does not catch the error lines | carries stdout only. Join them first: cmd 2>&1 | grep pattern.sort data.txt > data.txt leaves the file empty > empties the file before the program reads it. Write to another file first, then rename it with mv.> only touches fd 1. That is trap number one.2> Two streams, two operators, two places.2>&1 > f 2>&1 gathers both; 2>&1 > f leaves stderr on the screen. Step through to see why.2>&1 | before grep, so grep can filter the complaints.