Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script with loop like

while read host
./script &

I want to know the process id of the current process which is starting the ./script in background and use it in script.

share|improve this question
but $$ does not work inside script which is in background – munish Apr 15 '13 at 14:06
Oops! sorry goldi i nade a mistake mistakenly i edited your comment – munish Apr 15 '13 at 14:07
You didn't; I deleted it and made it an answer. – goldilocks Apr 15 '13 at 14:10
up vote 6 down vote accepted

Assuming a POSIX shell (like bash), $$ is the PID of the current shell and $PPID is the PID of the parent. You can pass $$ to the script or check the value of $PPID in the script.

share|improve this answer

The process ID of the current shell is in the special variable $$.

You can pass this to a forked (backgrounded) process:

./script $$ &

In "script", that will be the first argument, so:


There's a summary of bash special variables here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.