Bash: Capture Output of Command Run in Backgroundfleeting
- External reference: https://stackoverflow.com/questions/20017805/bash-capture-output-of-command-run-in-background/20018118
Bash: Capture output of command run in background - Stack Overflow
The solution is the following construct
$ exec 3< <(yes) # Save stdout of the ‘yes’ job as (input) fd 3.
This opens the file as input fd 3 before the background job is started.
You can now read from the background job whenever you prefer. For a stupid example
$ for i in 1 2 3; do read <&3 line; echo “$line”; done y y y
Note that this has slightly different semantics than having the background job write to a drive backed file: the background job will be blocked when the buffer is full (you empty the buffer by reading from the fd). By contrast, writing to a drive-backed file is only blocking when the hard drive doesn’t respond