[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

$$ not always returning the current PID

From: Kees Hoekzema
Subject: $$ not always returning the current PID
Date: Fri, 22 Feb 2002 02:54:48 +0100

Lectori Salutem,

During the write of a bash shell script I encountered the following problem,
i really don't know if i should call it a bug.

The script which demonstrates the problem is:

echo_pid() { echo "(Inner script) ${$}"; }

echo "Outer script: ${$}"
echo_pid &
echo "Outer script: ${!}"

Which gives me the following output:

Outer script: 9809
Outer script: 9810
(Inner script) 9809

As you can see, the first time $$ is called, it returns the PID of the
shell, in this case 9809. Directly after that it starts the echo_pid in the
background and echos the PID of the last started job in background, in this
case 9810, so the PID that the function should return is 9810, but it
doesn;t do that, it returns the PID of the first shell, which is 9809.

If you use a eternal loop in the echo_pid function, you can see the first
script finish nicely, if you do a 'ps auxwwf' you still can see the function
running in the background and still returning the wrong PID.

hope someone can explain this (imho) strange behaviour :-)


root# echo $BASH_VERSION

reply via email to

[Prev in Thread] Current Thread [Next in Thread]