On Thu, Apr 28, 2016 at 10:38:53AM -0600, Eric Blake wrote:
> Bash has code to special-case 'jobs |' when it can obviously tell that
> you are running the jobs builtin as the sole command of the left side of
> a pipe, to instead report about the jobs of the parent shell,

Oh, that's interesting.  I didn't know that.

> but that
> special-case code cannot kick in if you hide the execution of jobs,
> whether by hiding it inside a function as you did, or by other means
> such as:
> $ eval jobs | grep vim

In general, if you want to filter the output of "jobs" or some other
builtin that changes its behavior when invoked in a subshell, then
you need to avoid the subshell.  That means no pipeline, no command
substitution, etc.  Basically that leaves you with a temporary file.

tmpfile=...  # boilerplate code to create a temp file on whatever OS
trap 'rm -f "$tmpfile"' EXIT
jobs > "$tmpfile"
if grep -q vim "$tmpfile"; then ...

Reply via email to