ls -la /dev/tty shows the output:

crw-rw-rw- 1 root tty 5, 0 Dec 14 22:21 /dev/tty

What does c at the beginning mean? When I do something like pwd > /dev/tty it prints to the stdout. What does the file /dev/tty contain?

share|improve this question

closed as off-topic by Corey Ogburn, Trinimon, Heikki, Mark Hildreth, DuckMaestro Dec 23 '13 at 22:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about general computing hardware and software are off-topic for Stack Overflow unless they directly involve tools used primarily for programming. You may be able to get help on Super User." – Corey Ogburn, Trinimon, Heikki, Mark Hildreth, DuckMaestro
If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 64 down vote accepted

The 'c' means it's a character device. tty is a special file representing the 'controlling terminal' for the current process.

Character Devices

Unix supports 'device files', which aren't really files at all, but file-like access points to hardware devices. A 'character' device is one which is interfaced byte-by-byte (as opposed to buffered IO).

TTY

/dev/tty is a special file, representing the terminal for the current process. So, when you echo 1 > /dev/tty, your message ('1') will appear on your screen. Likewise, when you cat /dev/tty, your subsequent input gets duplicated (until you press Ctrl-C).

/dev/tty doesn't 'contain' anything as such, but you can read from it and write to it (for what it's worth). I can't think of a good use for it, but there are similar files which are very useful for simple IO operations (e.g. /dev/ttyS0 is normally your serial port)

This quote is from http://tldp.org/HOWTO/Text-Terminal-HOWTO-7.html#ss7.3 :

/dev/tty stands for the controlling terminal (if any) for the current process. To find out which tty's are attached to which processes use the "ps -a" command at the shell prompt (command line). Look at the "tty" column. For the shell process you're in, /dev/tty is the terminal you are now using. Type "tty" at the shell prompt to see what it is (see manual pg. tty(1)). /dev/tty is something like a link to the actually terminal device name with some additional features for C-programmers: see the manual page tty(4).

Here is the man page: http://linux.die.net/man/4/tty

share|improve this answer

/dev/tty is a synonym for the controlling terminal (if any) of the current process. As jtl999 says, it's a character special file; that's what the c in the ls -l output means.

man 4 tty or man -s 4 tty should give you more information, or you can read the man page online here.

Incidentally, pwd > /dev/tty doesn't necessarily print to the shell's stdout (though it is the pwd command's standard output). If the shell's standard output has been redirected to something other than the terminal, /dev/tty still refers to the terminal.

You can also read from /dev/tty, which will normally read from the keyboard.

share|improve this answer
    
I can't understand your pwd > /dev/tty doesn't necessarily print to stdout. If standard output has been redirected to something other than the terminal. In my opinion, it will always set file descriptor 1 of the process to /dev/tty. Could you explain? – Dagang Feb 25 '14 at 10:01
    
@dagang: Your opinion is mistaken. If standard output is redirected to a file, file descriptor 1 will refer to that file, not to the terminal. That's what redirection means. – Keith Thompson Feb 25 '14 at 14:44
    
Yes, fd 1 can refer to any file, but >/dev/tty always means make fd 1 refer to the terminal, regardless of where fd 1 was referring to. Could you provide an example to support your argument? – Dagang Feb 26 '14 at 4:40
    
@dagang: Ah, I see where the confusion comes from. Yes, pwd always prints to stdout, and yes, in this case stdout goes to /dev/tty. My point was that that's not necessarily the standard output of the shell. (Though the fact that pwd is necessarily a shell builtin probably complicates things.) – Keith Thompson Feb 26 '14 at 5:19
2  
An example of how it makes a difference: ( echo to stdout ; echo to tty > /dev/tty ) > a_file One will find "to stdout" to be redirected to a_file, but "to tty" will still appear on the terminal the shell is running from and not be redirected to a_file. The sub-shell running the part inside the ( ... ) has stdout redirected to a file but the controlling terminal the still whatever terminal the parent shell is running in, e.g.. your xterm or ssh or serial console, etc. – TrentP Mar 21 '17 at 2:21

The 'c' means it's a character special file.

share|improve this answer

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