I'm trying to use bash to open a new descriptor for writing extra diagnostic messages. I don't want to use stderr, because stderr should only contain output from the programs called by bash. I also want my custom descriptor to be redirectable by the user.
I tried this:
exec 3>/dev/tty
echo foo1
echo foo2 >&2
echo foo3 >&3
But when I try to redirect fd 3, the output still writes to the terminal.
$ ./test.sh >/dev/null 2>/dev/null 3>/dev/null
foo3
First the parent shell sets file descriptor 3 to /dev/null
Then your program sets file descriptor 3 to /dev/tty
So your symptoms are not really surprising.
Edit: You could check to see if fd 3 has been set:
if [[ ! -e /proc/$$/fd/3 ]]
then
exec 3>/dev/tty
fi