9

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

mytty=$(tty)
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

1 Answer 1

15

Try

... | tee /dev/tty

Like:

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

The command tty doesn't work inside a pipe:

$ echo $(tty)           # or (a lot better) simply:  tty
/dev/pts/4

$ 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
    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 .