On Sat, 28 Feb 2026, 17:30 Ben Ashton, <[email protected]> wrote:
> …
> We now have sleep running as a child of the subshell as a process group
> leader.
> However in certain, somewhat contrived, circumstances, bash's builtin kill
> will send the signal to the WRONG process group.
>
> > Sending SIGTERM to process group: 5710 (kill -TERM -5710)
> [pid 5709] kill(-5709, SIGTERM) = 0
>
> As you can see we ran `kill -TERM -5710`, but bash actually sent it
> to process group 5709 (the subshell itself). If you don't use the
> builtin kill then it works as expected
> …
> Here is the script to reproduce the issue:
>
>
>
> setsid sleep 10 &
> pid=$!
> echo "> Started new process group $pid"
…
echo "> Sending SIGTERM to process group: $pid (kill -TERM -$pid)"
> kill -TERM -$pid
>
This is the core error: $! records a PID, NOT a PGID or a SID. On some
systems (e.g. Linux) a PGID or SID is always numerically equal to the PID
of the leader process, on others (e.g. BSD) they are unequal.
So what would you expect to happen if you go «kill -INT -$pid» on a systems
where pgrps and their respective leader pids are not identical?
Would you want it to translate the PID to its pgrps, or would you prefer it
to fail?
If it's going to do translations on BSD, why should it NOT do so on Linux?
-Martin