I've read that when redirecting stdout and stderr to the same file using 2>&1, stdout is usually block buffered while stderr is unbuffered.

If a very long line is written to stdout, requiring two separate flushes of the buffer, could it happen that a line from stderr slips in the middle? Something like this:

stdout:   aaaaaaaa.....really long line......aaaaaaaaaaaa<newline>

stderr:   eee<newline>

combined: aaaaaaaaaaaaaaaaeee<newline>

Yes, exactly that can happen if lines to stdout are long enough.

use strict;
use warnings;

for (1..10) {
    print "START"; print "-" x 100000; print "END\n";
    warn "!\n";


./writer.pl > out 2>&1

Checking: Open file out in an editor and find stderr ! in between -, not always between END and START

This will vary between OS:s, languages and systems in various ways, but your basic assumption is correct. Experiment by varying length of stdout-lines: print "-" x 100, print "-" x 10000 etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.