1

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?

1 Answer 1

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .