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.
Asked
Active
Viewed 1,175 times
1 Answers
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.
Stabledog
- 252
-
1See also here (https://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 Nov 09 '21 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. – peterh Jan 11 '20 at 20:51/proc/selfis a file.$selfis a variable. Not the same thing. – ctrl-alt-delor Jan 11 '20 at 22:43