How to get the process id of a bash subprocess on command line

后端 未结 6 1312
庸人自扰
庸人自扰 2020-12-15 03:47

I know in bash we can create subshells using round parenthesis ( and ). As per bash man page:

(list) list  is  executed  in  a  subs         


        
6条回答
  •  隐瞒了意图╮
    2020-12-15 03:59

    This seems like it works:

    (echo $$; echo  `ps axo pid,command,args | grep "$$" |awk '{ getline;print $1}'`)
    14609
    17365
    

    For whatever reason, OSX is limited and doesnt come with pgrep, or one could do (which works in Linux):

     (echo $$; echo  `pgrep -P $$`) 
     14609
     17390
    

提交回复
热议问题