Skip to contentSkip to conversation

Inconsistent behaviour between 'jobs' and 'echo "$(jobs)"'

DISCUSSION Damian Wrobel 2 participants 4 messages Jan 19, 2015 – Jan 21, 2015 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hi,

I'm observing an inconsistent behaviour between:
jobs
and
echo "$(jobs)"

Here is the short example:

$ rpm -qv dash
dash-0.5.8-1.fc21.i686
$ /bin/dash
$ sleep 1000 &
$ jobs
[1] + Running                    sleep 1000
$ echo "$(jobs)"
$ echo `jobs`
$ jobs 2>/dev/null
[1] + Running                    sleep 1000
$
Signature · 2 lines
Have a nice day,
Damian
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Seb

On Mon, Jan 19, 2015 at 07:01:53PM +0100, Damian Wrobel wrote:

Hello,

Quoted 4 lines

I'm observing an inconsistent behaviour between:
jobs
and
echo "$(jobs)"

Quoted 6 lines

It's because the command is ran in a sub-shell, where there is indeed no running job.

Bash has a special mechanism to handle this and get the current shell context returned, that's why you may feel some inconsistency here (like I myself did :)

There is an application usage [1] where this case is specifically mentioned with a suggestion that: "For this reason, jobs is generally implemented as a shell regular built-in."

I basically planned to use the following construction to kill all running jobs:

$ kill $(jobs -p)

Now it looks that even the following doesn't work in a dash:

$ jobs -p | xargs kill

I would prefer not to code something like the following:

$ jobs -p >/tmp/jobs-$$ && kill $(cat /tmp/jobs-$$); rm /tmp/jobs-$$

Have a nice day,
Damian

[1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/jobs.html

Quoted 6 lines

++
Seb.

#3 lore raw
Quoted 13 lines · Damian Wrobel
Quoted 12 lines · Seb

On Mon, Jan 19, 2015 at 07:01:53PM +0100, Damian Wrobel wrote:

Hello,

Quoted 4 lines

I'm observing an inconsistent behaviour between:
jobs
and
echo "$(jobs)"

It's because the command is ran in a sub-shell, where there is indeed no running job.

Quoted 7 lines
Quoted 3 lines

Bash has a special mechanism to handle this and get the current shell context returned, that's why you may feel some inconsistency here (like I myself did :)

There is an application usage [1] where this case is specifically mentioned with a suggestion that: "For this reason, jobs is generally implemented as a shell regular built-in."

Indeed, it seems the standard requires, or at least expects this to work:

"The -p option is the only portable way to find out the
process group of a job because different implementations have different strategies for defining the process group of the job. Usage such as $(jobs -p) provides a way of referring to the process group of the job in an implementation-independent way."

Quoted 3 lines

Now it looks that even the following doesn't work in a dash:

$ jobs -p | xargs kill

For the same reason as above: each member of the pipe is ran in its own sub-shell.

Quoted 3 lines

I would prefer not to code something like the following:

$ jobs -p >/tmp/jobs-$$ && kill $(cat /tmp/jobs-$$); rm /tmp/jobs-$$

I agree it's not very nice.

++
Seb.

#4 lore raw
In reply to Damian Wrobel
Quoted 3 lines · Damian Wrobel

I would prefer not to code something like the following:

$ jobs -p >/tmp/jobs-$$ && kill $(cat /tmp/jobs-$$); rm /tmp/jobs-$$

As a better alternative, you could maybe try this:

trap : TERM # in case we have something to do after... /bin/kill -s TERM -$$

Just an idea...

++
Seb.