To read from file descriptor 6 I can use
/proc/self/fd/6). Usually both work equally well. However if that file descriptor happens to be a socket, strange things happen. For example:
$ bash -c 'ls -l /dev/fd/6;cat /dev/fd/6' 6</dev/tcp/localhost/12345 lrwx------ 1 michas michas 64 Jan 10 19:50 /dev/fd/6 -> socket: cat: /dev/fd/6: No such device or address
ls shows the descriptor is indeed present. But accessing the data is not possible this way. If I use
cat <&6 instead everything works well again.
What is the difference between both ways of accessing the file descriptor?
Is there a good way to access a descriptor if the number if given in a variable? (
</dev/fd/$fd would work, but
<&$fd does not.)
(The above situation can be observed on linux, but not on OpenBSD. - Seems like that file descriptor is a regular character device there.)