Capture output of command triggered in background

Is there any way to trigger a sequence of commands in parallel and capture their output in variables? e.g. something on the following lines

x=`echo "X" &`
y=`echo "Y" &`
z=`echo "Z" &`

so that $x, $y, and $z evaluate to X, Y and Z res.

Why evaluate in parallel? Sequential evaluation will result in the variables being set as you state. Perhaps you are trying to do something different?

Yes you could use process substitution with exec:

exec 4< <(sleep 4s; echo A)
read -u 4 __
echo "$__"
exec 4<&-

If you're going to execute an external binary use exec:

exec 4< <(exec yes)

I am not really running echo statements :slight_smile: that was just an example. The actual statements are function calls to SQL procedures which can take 10-15 minutes to complete. I want to trigger these in parallel to speed up execution of my job.

Although a little convoluted you couid save each, (set of?), result(s) to disk and then read the contents of each saved file at your leisure...

Just an alternative thought...

Yes my example was just an example as well. You could use your sql commands there. If you intend to send input instead you could make use of input process substitution instead (>()).

I'd guess that due to resource competition the resulting overall time will not be much faster than the serial execution.

I am sorry it isn't clear from the message, I was replying to the message above which suggested using sequential commands for this. I will try your method and see it if suits my need. Thanks for your help.