Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between /proc/self and /proc/$$?

Tags:

linux

bash

shell

i used to think that /proc/self and /proc/$$ are the same in bash terminal, but now i find they are different.

i know $$ means the current process' pid, and /proc/self is the currently running process, it should be bash terminal. why they are different?

root@VM-73-203-debian:~# echo $$
24415
root@VM-73-203-debian:~# ls -l /proc/self
lrwxrwxrwx 1 root root 0 Nov 22  2018 /proc/self -> 24465
root@VM-73-203-debian:~# ls -l /proc/$$
total 0
dr-xr-xr-x 2 root root 0 May 29 16:23 attr
...
like image 575
peter zhang Avatar asked Mar 04 '23 22:03

peter zhang


1 Answers

$$ is a special bash variable that gets expanded to the pid of the shell.

/proc/self is a real symbolic link to the /proc/ subdirectory of the process that is making the call.

When you do ls /proc/$$ the shell expands it to ls /proc/pid-of-bash and that is what you see, the contents of the shell process.

But when you do ls /proc/self you see the contents of the short lived ls process.

The $$ is not limited to this usage, you can write echo $$ to see the bash pid; you can use it to kill yourself, etc.

like image 178
rodrigo Avatar answered Mar 10 '23 23:03

rodrigo