Search code examples
linuxstdoutstdinstderrtty

what's the relationship between /dev/tty and stdin/stdout/stderr?


I know they have different file descriptors, stdxx can be redirected.

int tty_fd = open("/dev/tty", O_RDWR);

So, what's the difference in the codes below(stdxx not redirected):

n = read(tty_fd, buf, sizeof(buf));
n = read(STDIN_FILENO, buf, sizeof(buf));

n = write(tty_fd, buf, sizeof(buf));
n = write(STDOUT_FILENO, buf, sizeof(buf));

is stdxx same as a link to /dev/tty ?


Solution

  • Unless there is no redirection "/dev/tty" would be used to display both the standard-output or standard-error. You can selectively output your stdout/stderr onto different file descriptors other than /dev/tty.