I am struggling with understanding IO redirection in bash. I have seen many examples like the following (which is literally taken from the accepted answer here):
exec 3<> /tmp/foo #open fd 3.
echo "test" >&3
exec 3>&- #close fd 3.
I don't understand the second line. To be precise, I don't understand how its behavior is in accordance with the bash manual.
Obviously, in the second line, >&3
should redirect stdout
(and possibly stderr
- I don't know) to file descriptor 3; everything else wouldn't make any sense.
But from the current bash manual (at the time of writing), section 3.6.4 (formatting mine):
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word. There are two formats for redirecting standard output and standard error:
&>word
and>&word
[ It then explains subtle differences between the first and the second format, but let's put this aside for a moment because it is not important for the question. ]
To my understanding this says that >&3
redirects stderr
and stdout
to a file whose name is 3
, which is quite different from redirecting stdout
(and possibly stderr
) to file descriptor 3
.
Could anybody please explain what I am missing?