Get pid of current subshell

后端 未结 6 810
眼角桃花
眼角桃花 2020-12-08 07:10

I am trying to get the pid of a currently executing subshell - but $$ is only returning the parent pid:

#!/usr/bin/sh

x() {
  echo \"I am a sub         


        
6条回答
  •  悲&欢浪女
    2020-12-08 07:49

    If you use Linux-kernel, you can use Linux-kernel's /proc/self feature to do this:

    In simplest form: cd -P /proc/self && basename "${PWD}"

    To keep the PWD and OLDPWD variable: PWD_BACKUP="${PWD}";OLDPWD_BACKUP="${OLDPWD}";cd -P /proc/self && basename "${PWD}";cd "${PWD_BACKUP}";OLDPWD="${OLDPWD_BACKUP}"

    For example:

    $ cd -P /proc/self && basename "${PWD}"
    26758
    $ (cd -P /proc/self && basename "${PWD}")
    26959
    $ (cd -P /proc/self && basename "${PWD}")
    26961
    $ 
    

提交回复
热议问题