Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Jan 2022 23:33:34 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 261200] Unable to pipe the output of jobs in sh
Message-ID:  <bug-261200-227-RUw9QjvrG5@https.bugs.freebsd.org/bugzilla/>
In-Reply-To: <bug-261200-227@https.bugs.freebsd.org/bugzilla/>
References:  <bug-261200-227@https.bugs.freebsd.org/bugzilla/>

next in thread | previous in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D261200

--- Comment #6 from Jilles Tjoelker <jilles@FreeBSD.org> ---
The reason that jobs cannot be piped is that an element of a pipeline (with
more than one element) is run in a subshell environment, and the subshell
environment has its own jobs. For example,

sh -c ':& { :& jobs; }|wc -l'

writes 1.

There are, however, some exceptions to this.

One such exception is that if a command substitution contains a single jobs
command, this jobs command returns information about the parent shell
environment. This exception is documented in the man page under "Command
Substitution". The command is otherwise still executed in a subshell
environment, so, for example, variable assignments in expansions do not
persist. Technically, this is implemented by executing certain command
substitutions in the same process; among other things, resetting the jobs t=
able
is skipped and anything that would alter it does not follow this code path.

This exception is commonly available (like the one for `trap` which has an
Austin Group interpretation: https://www.austingroupbugs.net/view.php?id=3D=
53 ),
but is not always implemented the way FreeBSD sh implements it. Some other
shells instead implement it by making `jobs` (or `trap`) return the informa=
tion
from just before the subshell environment was entered if no change had been
made yet, but in the case of `jobs` this is a bit unfortunate: either it
creates an observable difference between (non-special) builtins and external
programs, or it requires trickery to ensure a foreground job can be run wit=
hout
disturbing the jobs table for display by `jobs`.

In bash (5.1.16(0)-release), `:& jobs | wc -l` and `:& J=3Djobs; $J | wc -l`
work, but `:& { jobs; } | wc -l` does not, so bash appears to use a similar
analysis like FreeBSD sh uses for command substitutions.

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-261200-227-RUw9QjvrG5>