LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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.
 
Old 01-18-2016, 07:42 AM   #12
anindyameister
Member
 
Registered: Oct 2012
Posts: 47

Rep: Reputation: Disabled
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

HELLO
6
I can see the output of both the full output of the first cmd as well as the output of the wc running on it.

Last edited by anindyameister; 01-18-2016 at 07:44 AM. Reason: Elaboration
 
Old 01-18-2016, 12:25 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The tty command looks for the device connected to its standard input. When you run
Code:
some_command | tee $(tty)
the pipe is set up first, so the tty command sees the pipe as its standard input, hence, "not a tty".

It's a lot easier just to use /dev/tty, which is the controlling terminal for the process regardless of I/O redirections.
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration