Thanks, I see now. However when you give wait a deliberately bogus PID the
behaviour is different:
a=$(wait 9999)
echo $? # prints 127
...why is this different? Is there something special about $! in a brand new
subshell then?
OK. A command substitution is executed in what POSIX calls a `subshell
environment', which is an exact copy of the parent shell's execution
environment. This includes background jobs and $!, and their saved exit
statuses.
The command substitution thinks the value of $! is valid, so the `wait'
builtin doesn't reject it as invalid. If that process had already exited
by the time the command substitution runs, the subshell execution
environment knows its exit status, and wait will return it for you.
If not, the shell has to wait for it, but since it's not a child of that
shell, waitpid(2) will return -1/ECHILD and that status will be reflected
in the wait builtin's return status.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/