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

前端 未结 6 1249
[愿得一人]
[愿得一人] 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:48

    use $BASHPID or $$

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

    TL;DRTFM

    • $$ Expands to the process ID of the shell.
      • In a () subshell, it expands to the process ID of the invoking shell, not the subshell.
    • $BASHPID Expands to the process ID of the current Bash process (new to bash 4).
      • In a () subshell, it expands to the process ID of the subshell [1]: http://www.gnu.org/software/bash/manual/bashref.html#Bash-Variables

提交回复
热议问题