How does a Linux/Unix Bash script know its own PID?

前端 未结 6 1258
[愿得一人]
[愿得一人] 2021-01-29 23:10

I have a script in Bash called Script.sh, and it needs to know its own PID (i.e. I need to get PID inside the Script.sh )

Any idea how to do this ?

6条回答
  •  不知归路
    2021-01-30 00:00

    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
    

提交回复
热议问题