Re: `jobs` within command substitution doesn't work

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 23 Oct 2024 at 20:04, Christoph Anton Mitterer
<calestyo@xxxxxxxxxxxx> 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
mike.squire@xxxxxxxxx





[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux