use Capture::Tiny 'tee'; my $output = tee { system('t=1;while [ 1 -eq 1 ]; do echo hello $t; if [ $t -eq 10 ]; then exit 0; fi; t=$((t+1)); sleep 1; done') }; print $_ while( <$output> );