Capturing output of a script to variable with streaming to console

I’m trying to execute a script and capture it’s output. Since it does not provide valid error codes and I cannot modify it, I have to rely on the captured stdout to verify the result of the script. So far I’ve tried:

output=$(my-script 2>&1 | tee /dev/tty)

This works as intended locally, but fails when ran via a Docker action with tee: /dev/tty: No such device or address.

As a workaround, I’ve also used printf to display the captured output, but it does not support continuous streaming of the results to console and displays the output after the script is fully executed. This is something that I’d like to avoid, if possible.

output=$(my-script 2>&1 )
printf "%s\n" "${output[@]}"

Is there any way that I can modify my bash script or Dockerfile to achieve this effect?

How about you do it the other way around, and read the variable from a file after the command is complete?

my-script 2>&1 | tee "${tmp}"
output="$(cat "${tmp}")"
rm "${tmp}"

This seems to work, thanks.

1 Like