tee and assigning to variable?

Solution 1:

You would do this:

myvar=$( mycommand | tee /dev/tty | grep -c keyword )

Use tee to pipe the output directly to your terminal, while using stdout to parse the output and save that in a variable.

Solution 2:

You can do this with some file descriptor juggling:

{ myvar=$(mycommand | tee /dev/fd/3 | grep keyword); } 3>&1

Explanation: file descriptor #0 is used for standard input, #1 for standard output, and #2 for standard error; #3 is usually unused. In this command, the 3>&1 copies FD #1 (standard output) onto #3, meaning that within the { }, there are two ways to send output to the terminal (or wherever standard output is going).

The $( ) captures only FD #1, so anything sent to #3 from inside it will bypass it. Which is exactly what tee /dev/fd/3 does with its input (as well as copying it to its standard output, which is the grep command's standard input).

Essentially, FD #3 is being used to smuggle output past the $( ) capture.