preview
In the code below we wait for a background process three times. In the first paragraph we get the expected 0 return code from the wait. In the second paragraph the only change is wrapping the wait in a command substitution which instead gives a 255 return code. In the third paragraph the only change is wrapping the wait in a subshell which gives a 127 return code. Why is this?
sleep 1 & wait $! echo $? # prints "0" sleep 1 & a=$(wait $!) # <---- only difference is cmd substitution echo $? # prints "255" sleep 1 & (wait $! &>/dev/null) # <---- only difference is subshell echo $? # prints "127"
Thanks in advance for your thoughts!
EDIT: updated to include the subshell final example also, which gives a clue
In the code below we wait for a background process three times. In the first paragraph we get the expected 0 return code from the wait. In the second paragraph the only change is wrapping the wait in a command substitution which instead gives a 255 return code. In the third paragraph the only change is wrapping the wait in a subshell which gives a 127 return code. Why is this?
sleep 1 & wait $! echo $? # prints "0" sleep 1 & a=$(wait $!) # <---- only difference is cmd substitution echo $? # prints "255" sleep 1 & (wait $! &>/dev/null) # <---- only difference is subshell echo $? # prints "127"
Thanks in advance for your thoughts!
EDIT: updated to include the subshell final example also, which gives a clue