简体   繁体   中英

Capturing output of asynchronous bash commands?

Is it possible to do something like this in bash?

output=$(echo hello; sleep 10) &
pid=$!
# run some more commands
wait $pid
echo $output

I would like to run multiple commands in parallel, capturing their output. I'd like to do it in a "clean" way - not writing to temporary files etc.

You can use a named pipe to communicate between parent and child, see man mkfifo for details. Parent can use read command to fetch data from the pipe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM