6

How do I run a sequence of commands in parallel, and store their output in a variable?

I've tried:

output=`(echo -n "started "; sleep 2; echo "stopped") &`

echo "output before=$output"
wait
echo "output after=$output"

And got a pause of two seconds, followed by:

output before=started stopped
output after=started stopped

I expected:

output before=
<2 seconds pause>
output after=started stopped

How do I run a sequence of commands in the background, and store their output in a variable?

3
  • 1
    All block within () is executed on background, so you get its output once everything is finished. Hence, the sleep is "hidden" to you. Commented Oct 14, 2013 at 11:16
  • What are you really trying to do? This example looks constructed to illustrate an issue that may not be the best way to solve the problem you really have. Commented Oct 14, 2013 at 12:08
  • Run ~10 commands in parallel, wait for termination, and process their standard output separately. Commented Oct 14, 2013 at 12:22

2 Answers 2

6

From the manual:

If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background. The shell does not wait for the command to finish, and the return status is 0 (true).

A workaround would be to store the output in a file and read from it. The following might work for you:

tempout=$(mktemp)
( echo -n "started "; sleep 2; echo "stopped" ) > "${tempout}" &
echo "output before=$(<$tempout)"
wait
echo "output after=$(<$tempout)"
Sign up to request clarification or add additional context in comments.

1 Comment

There might be buffering issues involved here, where nothing is available yet to read from $tempout when the first echo is called. Depending on processor load, the background task may not even be started before the first echo.
0

I think the reason is the evaluation of this line: output=`(echo -n "started "; sleep 2; echo "stopped") &` Even though you want to put the whole command in the background (and this results in return code 0, as devnull mentioned) - but the result is not known. And to assign value to $output - it has to wait for the original command to exit.

You did not put var assignment in the background.

But still @devnull's workaround applies. Instead of files you may use named pipes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.