In a Bash script, I need to redirect the output of a command to a variable, while also streaming unbuffered output to the terminal.

I've tried this:

output=$(command 2>&1 | tee "$(tty)")

but this doesn't output anything to the terminal.

I've also tried

output=$(command 2>&1 | tee $mytty)

This gives me the error "tee: /dev/tty1: Permission denied".

The current OS is OpenSUSE 15.0 and I do not have sudo/root rights.


1 Answer 1



... | tee /dev/tty


output=$( command 2>&1 | tee /dev/tty )

The command tty doesn't work inside a pipe:

$ echo $(tty)           # or (a lot better) simply:  tty

$ echo aa | echo $(tty)
not a tty

Which means that the right side of a pipe is not connected to a tty.

  • 1
    Thank you. That works exactly the way I needed it.
    – jcs
    Commented Jun 25, 2019 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .