Why doesn't tee $(tty) produce output on the current terminal?
This came up in another thread and I'm confused. Why does the first use of tee produce output when the second one doesn't?
Code:
~$ this_terminal=$(tty) |
Because you have sent it to /dev/null ?
Code:
tred@vaio:~$ this_terminal=$(tty) |
Maybe I wasn't sufficiently clear: tee sends output to the files it is given as arguments, and to stdout. I would therefore expect echo hi | tee $(tty) to output "hi" twice, just as echo hi | tee $this_terminal does. The strings returned by $this_terminal and $(tty) are the same after all - I don't see why the two commands don't behave identically.
|
Does this help:
Code:
tred@vaio:~$ this_terminal=$(tty) |
Quote:
|
tee takes a file (or filedescriptor) as argument, but tty is actually a command. It will not be executed but instead be interpreted as a filename. Check "ls -la". The command "echo hi | tee $(tty)" will write "hi" into a file with the name of whatever variable $tty contains. If $tty contains nothing, there will be a file without a name in your current directory.
|
Quote:
Basically, the commands reduce to these: Code:
tred@vaio:~$ this_terminal=$(tty) So I am going to do a really "bad" thing, which is to send a PM to one of LQ's bash gurus to draw his attention to this thread, and maybe he'll see the light, because I am in darkness. Await developments..... |
I think almatic (X-post) has answered it, I'll hold off PM-ing.
Time for bed for me. Catch up later. |
Haha I've just noticed that
Code:
~$ echo hi | tee $(tty) |
Quote:
Quote:
|
Actually my explanation is nonsense, if it were true this_terminal=$(tty) wouldn't work. See here for the right explanation.
|
This is quite late, but I use the below method to print from within the pipeline while keeping the processing untouched
Code:
$ echo HELLO | tee /dev/$(ps ax | grep $$ | grep -v grep | awk '{print $2}')| wc -m |
The tty command looks for the device connected to its standard input. When you run
Code:
some_command | tee $(tty) It's a lot easier just to use /dev/tty, which is the controlling terminal for the process regardless of I/O redirections. |
All times are GMT -5. The time now is 11:59 PM. |