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.
1 Answer
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.
-
1See also here (man7.org/linux/man-pages/man5/proc.5.html) under the section "/proc/self". It says:
When a process accesses this magic symbolic link, it resolves to the process's own /proc/[pid] directory.Gabriel Staples– Gabriel Staples2021-11-09 16:46:30 +00:00Commented Nov 9, 2021 at 16:46
ls -l /proc/self/will be the/proc/<pid>directory of thelsprocess listing it. :-) Btw, doesn't the dupe answer your question? I think, yes./proc/selfis a file.$selfis a variable. Not the same thing.