command output to variable assignment and screen simultaneously

Hello Folks,

I have a script that runs a command (rsync) that sometimes takes a long time to complete and produces diagnostic output on stdout as it runs.

I am currently capturing this output in a variable and using it further in the script. I would like to continue to capture this output in a variable, but also display the output to the screen as it is produced from the command.

I am looking for a way to do this without creating a file on the filesystem. I am using Posix Shell on HP/UX and BASH on Linux (but open to other possibilities if necessary).

Currently I am doing this:

...
output=$(time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination" 2>&1)
echo "${output}"
...

Any suggestions will be appreciated,

-mbm

output=$(time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination" 2>&1 | tee /dev/tty)

Thanks jlliagre,

Man, talk about the forest hiding the trees! I tested this out, and it works great. I did have two hurdles. I'll explain them as others might have the same issues.

1) Wanted tty to be specified dynamically, and I tried originally with...

output=$(... | tee $(tty))

...but it wouldn't eval as the subshell wasn't attached to the tty. The obvious workaround was to set the tty to a variable first, then call it in the tee command:

tty=$(tty)
output=$(... | tee ${tee})

2) Since piping to tee yields a return code of 0 even if the rsync command fails, I have another isssue. Since I'm using Posix Shell and not BASH in HPUX, I don't have access to $PIPESTATUS.

Instead, I gathered the idea to prefix the rsync command with a trap to let me know if it failed like so:

output=$((trap 'RSYNC ERROR: $?' ERR && time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination) 2>&1 | tee ${tty})

Later on in the code, I can echo "${output}" | grep 'RSYNC ERROR' and parse out the return code if necessary.