0

Using zsh, if I try tab completion of the following command ls /proc/self/fd/ then I see the following:

files
0@   1@   10@  11@  12@  13   2@   3

However, when I press enter (without completing) I see:

> ls /proc/self/fd/
0  1  2  3

If I list the file descriptors for the current shell I get something different:

> ls /proc/$$/fd/
0  1  10  2

Why are these three results different?

Ben
  • 315

1 Answers1

1

Let’s start with ls /proc/self/fd: that shows the file descriptors open for the ls process. These are the three standard input, output and error descriptors, and the descriptor for the directory ls is reading.

ls /proc/$$/fd shows the file descriptors which are still open in the shell while ls is running: the three standard descriptors, and zsh’s copy of the terminal file descriptor (10).

Tab-completing ls /proc/self/fd/ shows the file descriptors open at that moment for the shell. In addition to those shown by ls /proc/$$/fd, this includes file descriptors open for the completion itself (to files in /usr/share/zsh/.../functions implementing the completion).

Stephen Kitt
  • 434,908
  • That's cleared things up a lot, thankyou! I have another question though; why does zsh need another file descriptor for the terminal? the 3 standard descriptors already point to the terminal. – Ben Aug 25 '20 at 14:43
  • Answer comments are not for follow-on questions, and we do in fact already have the answer to that in an answer to https://unix.stackexchange.com/q/434812/5132 . (-: – JdeBP Aug 25 '20 at 14:57
  • OK, but for anyone reading this and wondering the same thing; apparently zsh uses its own file descriptor for printing the prompt & command line, unlike bash which uses stderr for this. – Ben Sep 14 '20 at 21:54