0

I have this bash script:

echo "x" & X=$!
echo "y" & Y=$!
( echo "z" ; exit 0; ) & Z=$!  # this line

wait ${X} && echo "X $?" 
wait ${Y} && echo "Y $?" 
wait ${Z} && echo "Z $?" 

when I change exit 0 to exit 1, the last echo statement of the script does not execute. For example, here is the output when the exit code is 0:

x
X 0
z
y
Y 0
Z 0

but when I change it to 1:

x
y
X 0
z
Y 0

I am assuming what's in the parentheses should be executed in a subshell. Anyone know why my code does this?

4

As documented in help wait:

Returns the status of ID

So, if the process exited with 1, wait returns 1 and the && short circuits by not executing the second command. Try ; instead of && if you want to print the value regardless of success of the first command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.