First, yes, 1>&2
is the right thing to do.
Second, the reason your 1>&2 2>errors.txt
example doesn’t work is because of the details of exactly what redirection does.
1>&2
means “make filehandle 1 point to wherever filehandle 2 does currently” — i.e. stuff that would have been written to stdout now goes to stderr. 2>errors.txt
means “open a filehandle to errors.txt
and make filehandle 2 point to it” — i.e. stuff that would have been written to stderr now goes into errors.txt
. But filehandle 1 isn’t affected at all, so stuff written to stdout still goes to stderr.
The correct thing to do is 2>errors.txt 1>&2
, which will make writes to both stderr and stdout go to errors.txt
, because the first operation will be “open errors.txt
and make stderr point to it”, and the second operation will be “make stdout point to where stderr is pointing now”.