How to get PID of a backgroung prsc from that process???

It seems obviose, but I surprised to be not able to get it!

In bash, expected to get a current process ID by '$$'
So, if I ran backgroung any stuff and try from there get that process ID I would expect to have the same number as I have reported on foregroung screen, when I have started that background!
But it is not right!

/src> echo $$
14001
/src> { sleep 1; echo "sleepy-$$"; sleep 1; } &
[1] 8106
/src>
/src> sleepy-14001

[1]+  Done                    { sleep 1; /usr/bin/echo "sleepy-$$"; sleep 1; }

See, the backgroung process return the PID of parent process?! - 14001, while I would expect the 8106 ?!?!

How I should get the '8106' from inside of the curved brases?

The reason is that I need to run concurently many processes, which should use the same functions that need to use own files. I need separate files between processes and timestamp is not usefull.
I have plan to use the PID but..!!!

What I understand wrong about PIDs?
How I could get PID of any backgroung process from that process?

Thank you!

who said that a process will run with same PID when its ran in FG and in BG.??

Have you read the bash man page? If you search it for $$, you will find BASHPID.

{ sleep 1; echo "sleepy-$BASHPID"; sleep 1; } &

PID 14001 is your process id for the current SHELL
8106 is the PID of your background command.

use $! to get the PID of the last background job executed.

echo $!

-Devaraj Takhellambam

Does anyone know the way to get any process PID inside of that process?

Ok, example:
Exist a function FUNK that in processing create temporary file and call other functions that use that file.

From process-1 I start background processed B1, B2, B3

Each call the FUNC

I need to be able to separate the files that FUNC will create.

That why I need the PID of each process INSIDE of that process! NOT in the parent process where B1,B2,B3 has been started.
I need the FUNC be able to get the B1 PID when it is running in B1, B2 PID when the FUNC is called in B2 and B3 ... - the same.

(I do not see a reason to reply on 'very constructive' responses as 'if I ever read manual', or on pretention that I expect the same PID, or on advise to get it by parent process )

Did you read my post?

I repeat:

{ sleep 1; echo "sleepy-$BASHPID"; sleep 1; } &

cfajohnson - if you mean that the $BASHPID should give me the current PID - that is not clear from your statement.
Although I have check that, and my shell does not have this variable defined.
Also I have not found this variable in bash manual.

/src> echo $BASHPID

/src> echo "bacgroung : $BASHPID" &
[1] 4173
bacgroung :
[1]+  Done                    echo "bacgroung : $BASHPID"
/src>

I apologize.

The BASHPID variable is new to bash 4.0. It was introduced so that you can do what you want. Without it, you can't.

Unfortunately, I have no way to use version 4, here, where it should run, is used version 2.05

What would be another way (than $BASHPID) to get a background PID for function that is running in that backgroung process?
Is there any solution for, seems, so simple task?!?