views:

215

answers:

5

I have a script in bash called Script.sh, and i need to know his own PID ( i need to get PID inside the Script.sh )

Any clues to realize this ?

Regards,

Debugger

+3  A: 

use $BASHPID or $$

See the manual for more information, including differences between the two.

tvanfosson
Do note that $$ and BASHPID are not always the same thing - the manual mentions this, and there's a more concrete example here: http://tldp.org/LDP/abs/html/internalvariables.html . The distinction can be pretty important, as a lot of bash constructs do run in subshells.
Jefromi
@Jefromi -- noted. That was one of the reasons I linked to the manual.
tvanfosson
+5  A: 

The variable '$$' contains the PID.

Paul Tomblin
+1  A: 

You can use the $$ variable.

klausbyskov
+1  A: 

The PID is stored in $$.

Example: kill $$ will kill itself.

neo
A: 

In addition to the example given in the Advanced Bash Scripting Guide referenced by Jefromi, these examples show how pipes create subshells:

$ echo $$ $BASHPID | cat -
11656 31528
$ echo $$ $BASHPID
11656 11656
$ echo $$ | while read line; do echo $line $$ $BASHPID; done
11656 11656 31497
$ while read line; do echo $line $$ $BASHPID; done <<< $$
11656 11656 11656
Dennis Williamson