Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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’ve got approximately the following code:

cat infile | while read line; do
    echo 2> 'log ' $line
    echo $line
done > outfile

outfile is created correctly. However, the STDERR output just vanished: it’s neither displayed on the terminal nor in outfile. If I replace the last line above with done > outfile 2> errfile then errfile is created, but empty.

Can I capture the error output from within the loop, preferably by streaming it directly into the parent STDERR (the above is part of a larger script whose standard error stream is captured by yet another process)?

share|improve this question
up vote 4 down vote accepted

Your syntax is wrong: it should be >&2 not 2>.

share|improve this answer
Duh. Of course. – Konrad Rudolph Sep 7 '12 at 13:40

Your example doesn't work because inside the loop you are redirecting stderr with 2>. And then when you try to redirect it again outside the loop there is nothing left in the stream to redirect.

It would seem that this is what you actually wanted to do:

cat infile | while read line; do
    echo >&2 'log ' $line
    echo $line
done > outfile 2> errfile

This should redirect all errors from inside the while loop as expected. Unless you have already redirected errors inside the loop, as the example in your question.

share|improve this answer

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.