sh: fd redirections

Matthew Dillon dillon at apollo.backplane.com
Wed Dec 12 16:34:36 PST 2007


:Hi,
:
:I was testing a few things, and I was wondering if that behavior is normal:
:
:$ exec 3>&1
:$ echo toto >&3
:toto
:$ echo toto > /dev/fd/3
:toto
:$ exec 3>&-
:$ echo toto >&3
:toto
:$ echo toto > /dev/fd/3
:cannot create /dev/fd/3: Bad file descriptor
:
:Why is >&3 ignores the redirection, but not /dev/fd/3? It seems to me
:like a bug.
:
:SR

    I think the last part is working as it should.  You close descriptor 3
    with the 'exec 3>&-' line.   An echo that dup's to descriptor 3 ... that's
    only a temporary dup for just that command, so descriptor 3 shouldn't
    exist afterwords, and it doesn't.

    I'm not sure why descriptor 3 exists in the first part of your script,
    your original exec dup's descriptor 3 to descriptor 1.  Descriptor 3
    probably doesn't exist at that point so it probably wound up not changing
    descriptor 1 at all, and maybe that confused /bin/sh when you did the >&3
    later on and caused /bin/sh to fail to close the redirected descriptor.

					-Matt
					Matthew Dillon 
					<dillon at backplane.com>





More information about the Bugs mailing list