Skip to content
Advertisement

how to output only when command finish

I’m running multiple commands using &:

curl "url1" --output /dev/null 2>> out &
curl "url1" --output /dev/null 2>> out &
wait

This doesn’t work well because the output lines are interleaved. I want each curl to hold its output, until it’s done. How to do that?

I understand I can use temp files, concat together and delete afterwards. Are there better ways?

curl "url1" --output /dev/null 2>> out1 &
curl "url1" --output /dev/null 2>> out2 &
wait

Advertisement

Answer

You could execute each curl inside a subshell:

echo "$(curl "url1" 2>&1 >/dev/null)" >> out &
echo "$(curl "url2" 2>&1 >/dev/null)" >> out &
wait

2>&1 redirects stderr to stdout.
And yes, you can have nested "" with the subshell command $() 🙂

Downside: This may not preserve the order of outputs. In case the second curl completes first, it outputs first.

In order to keep the outputs ordered, you could use cat and <(). Bash’s process substitution <(COMMAND) creates a temporary file (actually, it’s a FIFO) from the output of COMMAND, but you don’t have to care about name, creation, and deletion of that temporary file.

cat 
<(curl "url1" 2>&1 >/dev/null) 
<(curl "url2" 2>&1 >/dev/null) 
> out 

The escapes the line breaks. Without the everything has to be written in one line.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement