Re: `jobs` within command substitution doesn't work
Mike Squire <[email protected]> Wed, 23 Oct 2024 20:46:46 +0100
| Newsgroups | org.kernel.vger.dash |
|---|---|
| Message-ID | <CAOn5hp+HSuut67JqqNXn9opCei4=aL-m+nrtFgRv7r8SjZqg-Q@mail.gmail.com> |
On Wed, 23 Oct 2024 at 20:04, Christoph Anton Mitterer <[email protected]> wrote: > but in dash: > $ sleep 60 & > $ jobs -p > 140049 > $ echo $(jobs -p) > > $ > > > Not really sure whether that’s behaviour which POSIX would allow, but > at least it makes it more or less impossible to easily get the PIDs of > and jobs within scripts. $! will give you the PID of the most recently backgrounded process and is part of the POSIX specification. So, using your example: $ sleep 60 & $ echo $! 1429004 $ jobs -p 1429004 I have often seen code like "some_command & pid=$!" to background a task and get its PID. It also has the advantage that it only gives you the PID of the most recently backgrounded task, not a list of all currently backgrounded tasks (which "jobs -p" does). HTH -- Mike Squire [email protected]