3 Answers
3

I don't think that's available in ksh. There's a POSIX solution which involves running an external process:

sh -c 'echo $PPID'

On Linux, readlink /proc/self would also work, but I fail to see any advantage (it might be marginally faster; it could be useful on a BusyBox variant that has readlink but not $PPID, but I don't think there is one).

Note that in order to get the value in the shell, you need to be careful not to run that command in a short-lived sub-sub-shell. For example, p=$(sh -c 'echo $PPID') might show the output of the subshell that invokes sh within the command substitution (or it might not, some shells optimize that case). Instead, run

I saw this suggestion already but it is not working. It gives me a third PID ... however I will test it again Monday when I'm back at work.
–
Patkos CsabaFeb 25 '12 at 15:45

@PatkosCsaba In ksh it would probably work because ksh optimizes forks, but in some other shells such as bash you need to be careful not to accidentally get the pid of a sub-sub-shell. See my updated answer.
–
GillesFeb 25 '12 at 20:08

You can achieve what you want, but you need to put run_something into a separate script. I'm not exactly sure why, but $$ is not re-evaluated when it is used in a function in the same script that is calling it. I guess that the value of $$ is assigned once after the script is parsed and before it is executed.

@mirabilos I'm stubborn about my preference for ==. No sympathy for [ + == users, and austingroupbugs.net/view.php?id=375 is still in the works anyway. If people are still using pdksh then I blame you. Work harder! The bigger issue is zsh's missing implicit -n even in emulation modes. Depending on my mood I also use ! ${var+false}, [[ ! ${var+_} ]], or [[ -v var ]] (which you don't support either. Get cracking!)
–
ormaajMay 2 '14 at 11:27