Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the second method proposed by this page, one gets the tty in which bash is being run with the command:

ps ax | grep $$ | awk '{ print $2 }'

I though to myself that surely this is a bit lazy, listing all running processes only to extract one of them. Would it not be more efficient (I am also asking if this would introduce unwanted effects) to do:

ps -p $$ | tail -n 1 | awk '{ print $2 }'

FYI, I came across this issue because sometimes the first command would actually yield two (or more) lines. This would happen randomly, when there would be another process running with a PID that contains $$ as a substring. In the second approach, I am avoiding such cases by requesting the PID that I know I want.

share|improve this question
5  
well most efficient of all would be run the tty command within your bash session. If you insist on using ps - ps -p $$ -o tty= – iruvar Mar 16 at 18:50
up vote 22 down vote accepted

Simply by typing tty:

$ tty 
/dev/pts/20

Too simple and obvious to be true :)

Edit: The first one returns you also the pty of the process running grep as you can notice:

$ ps ax | grep $$
28295 pts/20   Ss     0:00 /bin/bash
29786 pts/20   S+     0:00 grep --color=auto 28295

therefore you would need to filter out the grep to get only one result, which is getting ugly:

ps ax | grep $$ | grep -v grep | awk '{ print $2 }'

or using

ps ax | grep "^$$" | awk '{ print $2 }'

(a more sane variant)

share|improve this answer
    
Yes, that is the first suggested solution indeed (actually it is tty | sed -e "s:/dev/::"). But I think the second suggested solution on that page is not only inefficient, but also wrong! I'll use tty and stop complaining though. :) – Sh3ljohn Mar 16 at 18:54
    
The first one returns you also the pty of the process running grep as you can notice. – Jakuje Mar 16 at 19:00
2  
You don't need grep if you're using awk. e.g. ps ax | awk "/^$$/ {print \$2}" - note the use of double-quotes rather than single-quotes and the escaped \$2. But, as you say, using tty is the correct solution. – cas Mar 17 at 1:19
    
Grepping like that can result in false matches, e.g. if your PID is 10 and there's also a PID 103. Use awk -vpid=$$ '$1 == pid {print $2}' – Barmar Mar 23 at 17:32

If you want to be more efficient, then yes, you're right that ps can filter to just the process in question (and it will be more correct, not running the risk of picking up commands that happen to have your process number in their names). Not only that, but it can be told not to generate the header (option h), eliminating the tail process, and to display only the TTY field (option o tty), eliminating the awk process.

So here's your reduced command:

ps hotty $$
share|improve this answer

Other ways to do it:

readlink /dev/fd/0     #or 1 or 2 
readlink /proc/self/fd/0 #or 1 or 2
readlink -f /dev/stdin #or stdout or stderr; f to resolve recursively
#etc.

( If you're in a shell whose stdin, stdout and stderr are not connected to its controlling terminal, you can get a filedescriptor to the controlling terminal by opening /dev/tty:

( { readlink /dev/fd/0; } </dev/tty; ) </dev/null  >output 2>&1

)

Or with ps:

ps h -o tty -p $$ #no header (h); print tty column; for pid $$
share|improve this answer
2  
(1) I’m glad you realized that the current/controlling tty of a process is not associated with any file descriptor, and is not guaranteed to be open on any fd.  But, did you try the command that you offer as a workaround for that contingency?  When I try it, all I get is /dev/tty, and not the controlling tty (as reported by ps). (2) I believe that { readlink /dev/fd/0; } < /dev/tty is (and will always be) equivalent to readlink /dev/fd/0 < /dev/tty — do you believe otherwise?  If so, can you justify it? (3) The second semicolon in your command (the one after /dev/tty) is unnecessary. – Scott Mar 17 at 3:47

Just for completeness, while the other ps commands mentioned work on Linux, ps -p $$ -o tty= (as mentioned by @1_CR) is the most portable to all modern Unix systems, since it uses only options defined in the Unix Standard.

share|improve this answer

In the interactive shell, one could use w command for that. In the example bellow , w reports itself being run by specific user, and of course it shows TTY where that command is being ran.

$ w
 11:20:08 up 5 min,  3 users,  load average: 0.34, 0.39, 0.20
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
xieerqi  :0       :0               11:15   ?xdm?   1:47   0.34s init --user
xieerqi  pts/0    :0               11:18   23.00s  0.05s  0.05s /bin/mksh
xieerqi  pts/10   :0               11:20    0.00s  0.03s  0.01s w
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.