1

In this question: What is the file descriptor 3 assigned by default? is used /proc/self/fd, which dereference to /proc/32157/fd. So it is pid? And why cannot I echo $self? I have never seen self before.

  • It means always the current process. For example, an ls -l /proc/self/ will be the /proc/<pid> directory of the ls process listing it. :-) Btw, doesn't the dupe answer your question? I think, yes. – peterh - Reinstate Monica Jan 11 at 20:51
  • /proc/self is a file. $self is a variable. Not the same thing. – ctrl-alt-delor Jan 11 at 22:43
1

The /proc tree is a window into the operating system which is dynamically generated. When a process refers to /proc/self, the kernel translates self using the caller's pid. So that saves the process from doing a pid-lookup on itself, but you could get that same node of information by doing /proc/{pid} if you know the pid already.

One of the beauties of Unix is how it unifies all these things under / a root namespace and makes them behave like files and directories, even if they're not real physical file systems.

And you can't echo $self because that's a different concept entirely: to do an echo $[varname] is a shell thing: your shell has "environment variables" that maintain state. That has nothing to do with the /proc filesystem.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.