BEWARE, BASHISM AHEAD (there are posix shells that are significantly faster than bash, e.g. ash or dash, that don't have process substitution).
You can do a handle dance to move original standard output to a new descriptor to make standard output available for piping (from the top of my head):
exec 3>&1 # creates 3 as alias for 1
run_in_subshell() { # just shortcut for the two cases below
echo "This goes to STDOUT" >&3
echo "And this goes to THE OTHER FUNCTION"
}
Now you should be able to write:
while read line; do
process $line
done < <(run_in_subshell)
but the <()
construct is a bashism. You can replace it with pipeline
run_in_subshell | while read line; do
process $line
done
except than the second command also runs in subshell, because all commands in pipeline do.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…