HomePage RecentChanges Archive

UnderstandingShellRedirection

Stoppt die Vorratsdatenspeicherung! Jetzt klicken &handeln! Willst du auch an der Aktion teilnehmen? Hier findest du alle relevanten Infos
und Materialien:

Most newbies write the following if they wanna redirect STDOUT and STDERR into the same file:

command 2>&1 >/tmp/command.output

But what happens here is: The original STDOUT is written to the file and the original STDERR is written to the original STDOUT. The reason is a misunderstanding of what 2>&1 means.

The above can be written as:

command 2>&1 1>/tmp/command.output

In fact this means:

It does not mean:

When doing 2>&1 STDERR is not gone. There is still the file descriptor 2 (STDERR) which is redirected to where ever STDOUT is connected to. &1 is a reference, to what STDOUT is connected to. In other words, STDERR is not written to STDOUT, it is written to where STDOUT is written to.

The solution is

command 1>/tmp/command.output 2>&1

Which means: