2

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.

Stabledog
  • 252
Herdsman
  • 340
  • 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 Jan 11 '20 at 20:51
  • /proc/self is a file. $self is a variable. Not the same thing. – ctrl-alt-delor Jan 11 '20 at 22:43

1 Answers1

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
  • 1
    See 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