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.

2
  • 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. Commented Jan 11, 2020 at 20:51
  • /proc/self is a file. $self is a variable. Not the same thing. Commented Jan 11, 2020 at 22:43

1 Answer 1

2

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.

1
  • 1
    See 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. Commented Nov 9, 2021 at 16:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.