Why doesn't tee $(tty) produce output on the current terminal?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
tred@vaio:~$ this_terminal=$(tty)
tred@vaio:~$ echo hi | tee $this_terminal
hi
hi
tred@vaio:~$ echo hi | tee $(tty)
hi
tred@vaio:~$ echo $this_terminal
/dev/pts/1
tred@vaio:~$ echo hi | /dev/pts/1
bash: /dev/pts/1: Permission denied
bash: echo: write error: Broken pipe
tred@vaio:~$ echo hi > /dev/pts/1
hi
tred@vaio:~$ echo hi | tee /dev/pts/1
hi
hi
tred@vaio:~$
tred@vaio:~$ this_terminal=$(tty)
tred@vaio:~$ echo hi | tee $this_terminal
hi
hi
tred@vaio:~$ echo hi | tee $(tty)
hi
tred@vaio:~$ echo $this_terminal
/dev/pts/1
tred@vaio:~$ echo hi | /dev/pts/1
bash: /dev/pts/1: Permission denied
bash: echo: write error: Broken pipe
tred@vaio:~$ echo hi > /dev/pts/1
hi
tred@vaio:~$ echo hi | tee /dev/pts/1
hi
hi
tred@vaio:~$
?
Not really - I think the two highlighted lines should give the same output! Don't you?
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.
Not really - I think the two highlighted lines should give the same output! Don't you?
Well, yes, I do now.
Basically, the commands reduce to these:
Code:
tred@vaio:~$ this_terminal=$(tty)
tred@vaio:~$ echo $this_terminal
/dev/pts/1
tred@vaio:~$ echo hi | tee /dev/pts/1
hi
hi
tred@vaio:~$ echo hi | tee $(tty)
hi
tred@vaio:~$
This really doesn't make any sense to me but it is late here, and bash can be very precise, often for good reasons, once I have understood the bigger picture.
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.....
creates 3 files called "not", "a", and "tty" in my home directory, each with the contents "hi". I think I've got it - executing tty within a $(...) environment separates it from the parent terminal, so it returns "not a tty". I'm sure that could be said more precisely/accurately, but that kind of makes sense.
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.
No - tty is executed when it appears as $(tty). From the bash manpage:
Quote:
Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:
$(command)
or
‘command‘
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with
any trailing newlines deleted.
$(tty) expands to a string containing the output of tty - in this case "not a tty". It is not the same as $tty, which would indeed be a variable reference (or is it dereference?).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.