Shell – Can redirecting stdout and stderr to the same file mangle lines

io-redirectionshellstderrstdout

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>
          aaaaaaa<newline>

Best Answer

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

#!/usr/bin/perl
use strict;
use warnings;

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

Running:

./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.