如何获取我的脚本的PID?

Lau*_*rez 17 bash scripts

如果我有脚本“script1.sh”并且我想让它打印自己的 PID 作为第一步。我怎样才能在 bash 中做到这一点?

NGR*_*des 24

当前运行脚本的PID是 $$

http://www.gnu.org/software/bash/manual/bashref.html#index-_0024_0024

( $$) 扩展到 shell 的进程 ID。在()子shell 中,它扩展为调用shell 的进程ID,而不是子shell。

  • @trysis 在`bash` > 4 中,子shell 的PID(与任何shell 的PID 一样)存储在$BASHPID 中 (4认同)