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

前端 未结 6 1372
[愿得一人]
[愿得一人] 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-29 23:36

    If the process is a child process and $BASHPID is not set, it is possible to query the ppid of a created child process of the running process. It might be a bit ugly, but it works. Example:

    sleep 1 &
    mypid=$(ps -o ppid= -p "$!")
    

提交回复
热议问题