1

Command below prints pid of subshell and subshell of subshell:

$ ( ( echo $BASHPID )& echo $BASHPID )& sleep 1
[1] 9885
9885
9887
[1]+  Done                    ( ( echo $BASHPID ) & echo $BASHPID )

Now command below is more complicated, but it indicates that second subshell is in 'process group' of first subshell:

$ ( ( echo $$ $BASH_SUBSHELL $BASHPID ; export BBB=$BASHPID; ps -e -o pid,pgid,ppid,comm | grep -E "$$|$BBB|PGID" | grep -E "bash|PGID" )& echo $$ $BASH_SUBSHELL $BASHPID; sleep 1 )& sleep 1
[3] 9973
2787 1 9973
2787 2 9975
  PID  PGID  PPID COMMAND
 2787  2787  2769 bash
 9973  9973  2787 bash
 9975  9973  9973 bash

Is there simple way to create similar command which will show unique number for last row in second column?

noonex
  • 1,975
  • 1
  • 16
  • 18

1 Answers1

0

There's no way to do that since you are detaching the subshells from the current process (PGID) with the & operator therefore will run on a separate process (new PGID).

Check this ones also: How to set process group of a shell script

Community
  • 1
  • 1
LMC
  • 10,453
  • 2
  • 27
  • 52
  • but why first subshell has own PGID and second subshell - doesn't? (and belongs to fist subshell PGID). In other words I want to detach once again from first shell PGID – noonex Mar 31 '17 at 19:48
  • Your subshells are nested, the PPID of the outer one is the bash console itself, its PGID is its own one since its detached. I still wonder why the inner subshell does not have its own PGID tough. – LMC Apr 03 '17 at 13:43
  • Just realize I didn't answer your question :p – LMC Apr 03 '17 at 14:00