4

I am kind of new with bash and have been playing with it on and of for about a month.

While trying to understand how nested command groups work, I tried the following command:

((ps j; ps j); ps j; ps j)

Now, what I was expecting is that the nested group would produce a separate process group with a new bash shell as the group leader. A new bash shell is created but for some reason the nested bash shell is in the same process group as the bash shell above it.

Why is this? Is it perhaps because I am trying to view process information statically?

3
+50

As a first guess, I would assume that subshells started with ( .. ) don't use job control, in the same way that noninteractive scripts don't. However, $- seems to contain the m for job control even inside parenthesis (as well as i for interactive):

$ echo $-
himuBs
$ bash -c 'echo $-'
hBc
$ ( echo $-; )
himuBs

But I think that's a bit of a lie, since explicitly enabling job control makes some process groups appear.

These are all in one PG:

$  ( (ps j; ps j); ps j;) | awk 'NR == 1 || /[p]s/'
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
32524 32525 32522 32368 pts/23   32522 R+    1000   0:00 ps j
32524 32526 32522 32368 pts/23   32522 R+    1000   0:00 ps j
32522 32527 32522 32368 pts/23   32522 R+    1000   0:00 ps j

These aren't:

$ ( set -m; (ps j; ps j); ps j;) | awk 'NR == 1 || /[p]s/'
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
32518 32519 32518 32368 pts/23   32516 R     1000   0:00 ps j
32518 32520 32518 32368 pts/23   32516 R     1000   0:00 ps j
32516 32521 32521 32368 pts/23   32516 R     1000   0:00 ps j

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.