Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a perl script:

warn "1\n";
print "2\n";
warn "3\n";
print "4\n";

I pipe the output to a file:

perl &> foo

cat foo:


Why isn't the output in order, and how do I fix it?

share|improve this question
up vote 3 down vote accepted

STDERR is automatically flushed after printing a line, STDOUT is not. The STDOUT buffer is flushed only if it's full. To force autoflushing also on STDOUT use


(If you have an older perl, then the above may not work, instead you have to use something like $| = 1;)

share|improve this answer
Thanks, it works as expected if I autoflush both STDERR and STDOUT. – Will Sheppard Nov 26 '13 at 13:10
Will, please mark this answer as accepted. – Luís de Sousa Nov 26 '13 at 13:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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