0

This is a Linux-specific question.

The bash script is:

echo foo > "a.txt"
exec 3<"a.txt"
cat /dev/fd/3
cat /dev/fd/3
cat /dev/fd/3

Output:

foo
foo
foo

All these cats display the content of /dev/fd/3. But /dev/fd/3 is simply a symlink to a.txt. That explains the behavior but I don't know if it's guaranteed (and if so, by what) that:

  1. When you run exec 3<"a.txt", /dev/fd/3 is always a symlink to a.txt.

  2. Every time you open and read from /dev/fd/3, it returns the entire body.

There is an answer to a similar but not entirely same question:

https://unix.stackexchange.com/a/58124/7157

Cyker
  • 4,174
  • 6
  • 34
  • 45
  • `/dev/fd/3` is most definitely **not** a symlink to anything. – AlexP Dec 10 '19 at 10:01
  • @Kusalananda If you use `cat <&3` that's true, because the first `cat` moved the file pointer. But if you use `cat – Cyker Dec 10 '19 at 10:26
  • @AlexP It's what `ls -l` told me. Similar result on other's system: https://unix.stackexchange.com/questions/74454/somethings-special-about-dev-fd-3 – Cyker Dec 10 '19 at 10:28
  • 1
    @Cyker that's what the kernel tells `ls`, to be more friendly. It's not a link. The filesystem itself is virtual, so it's not even a real file. – muru Dec 10 '19 at 10:30
  • Poor `ls -l` had to find a way to show you what was the corresponding file, and that was the best it could do. It still isn't a symlink of any kind. It's just a name for the file descriptor #3 of process. – AlexP Dec 10 '19 at 10:33
  • @Cyker In any case, I still can't reproduce seeing the contents of `a.txt` three times. Unless you're in a shell that explicitly documents that `/dev/fd/3` is the same as "filedescriptor 3", there is no connection between `exec 3 – Kusalananda Dec 10 '19 at 10:33
  • @AlexP Yes the entire `/proc` filesystem is virtual. The point is if it is provided as a filesystem then it can be used like a filesystem. So if `/dev/fd/3` is shown as a symlink, then it can be used as a symlink. – Cyker Dec 10 '19 at 10:56
  • @Kusalananda That's weird. Have you tried `cat /proc/self/fd/3` instead? What does `ls -l /dev/fd /dev/fd/3 /proc/self/fd/3` show on your machine? – Cyker Dec 10 '19 at 10:59
  • 1
    @Cyker I don't have `/proc` (I'm not on Linux). The `/dev/fd/3` is a character special file (just like `/dev/tty`), definitely not a symbolic link. – Kusalananda Dec 10 '19 at 11:02
  • @Kusalananda Nice. I guess you are on Unix. This is a Linux question... I probably should edit the question adding this. – Cyker Dec 10 '19 at 11:03
  • Stéphane Chazelas answered this on this site back in 2012. – JdeBP Dec 10 '19 at 11:09
  • 1
    @muru stop bamboozling the OP. every file/system is "virtual" on linux. FWIW, yes, __on Linux__ opening `/dev/fd/N` -> `/proc/self/fd/N` -> `/path/to/the/original/file` will always open the original file it points to from scratch, and this is completely different from OpenBSD, Solaris, etc where `open("/dev/fd/N", ...)` will act just like `dup(N)`, ie will duplicate the fd. –  Dec 10 '19 at 11:09
  • @JdeBP Could you please link its address? I can add in question or mark as dup. Thanks – Cyker Dec 10 '19 at 11:13
  • https://unix.stackexchange.com/a/58124/5132 , https://unix.stackexchange.com/a/363072/5132 , https://unix.stackexchange.com/a/123659/5132 , and https://unix.stackexchange.com/a/296029/5132 just for starters. – JdeBP Dec 10 '19 at 13:19

0 Answers0