LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 03-17-2009, 02:27 PM   #1
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
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)
~$ echo hi | tee $this_terminal >/dev/null
hi
~$ echo hi | tee $(tty) >/dev/null
~$

Last edited by openSauce; 03-17-2009 at 02:29 PM.
 
Old 03-17-2009, 03:32 PM   #2
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Because you have sent it to /dev/null ?
Code:
tred@vaio:~$ this_terminal=$(tty)
tred@vaio:~$ echo hi | tee $this_terminal
hi
hi
tred@vaio:~$ echo hi | tee $(tty)
hi
tred@vaio:~$
 
Old 03-17-2009, 03:37 PM   #3
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
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.
 
Old 03-17-2009, 04:02 PM   #4
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Does this help:
Code:
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:~$
?
 
Old 03-17-2009, 05:11 PM   #5
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by tredegar View Post
Does this help:
Code:
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?
 
Old 03-17-2009, 05:45 PM   #6
almatic
Member
 
Registered: Mar 2007
Distribution: Debian
Posts: 547

Rep: Reputation: 67
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.
 
Old 03-17-2009, 05:48 PM   #7
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Quote:
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.....
 
Old 03-17-2009, 05:59 PM   #8
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
I think almatic (X-post) has answered it, I'll hold off PM-ing.
Time for bed for me. Catch up later.
 
Old 03-17-2009, 05:59 PM   #9
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Haha I've just noticed that
Code:
~$ echo hi | tee $(tty)
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.
 
Old 03-17-2009, 06:04 PM   #10
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by almatic View Post
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?).
 
Old 03-17-2009, 06:09 PM   #11
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Original Poster
Rep: Reputation: 39
Actually my explanation is nonsense, if it were true this_terminal=$(tty) wouldn't work. See here for the right explanation.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect make output to file, make | tee not working jiobo Linux - Software 5 05-11-2014 10:43 AM
LXer: Bash One-Liner Script To Produce Somewhat-Fancy Output Of Who's On Your Linux O LXer Syndicated Linux News 0 11-19-2008 01:40 AM
How do I produce sorted list of files in current directory over a certain size? bangback Linux - Newbie 3 10-25-2006 12:11 AM
xterm will not produce output with -lf parameter jarobman Linux - General 2 03-06-2005 02:12 AM
Running a program inside my QT application does not produce needed output files??? keos Programming 0 02-22-2004 11:37 PM


All times are GMT -5. The time now is 04:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration