ECHO setting is enabled on stdin and stdout by default. But why if we disable ECHO on stdin, it is also disabled on stdout? They have two separate descriptors - 0 and 1, so why they are changed simultaneously as if they had one and the same file descriptor?

The following program demonstrates this:

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
  struct termios tty_stdin;
  struct termios tty_stdin_restore;
  struct termios tty_stdout;
  tcgetattr(STDIN_FILENO, &tty_stdin);
  tcgetattr(STDIN_FILENO, &tty_stdin_restore);

  /* disable echo on stdin */
  tty_stdin.c_lflag &= (tcflag_t) ~ECHO;
  tcsetattr(STDIN_FILENO, TCSANOW, &tty_stdin);

  /* observe that it was automatically disabled on stdout */
  tcgetattr(STDOUT_FILENO, &tty_stdout);
  printf("STDOUT ECHO after changing STDIN: %d\n", tty_stdout.c_lflag & ECHO ? 1 : 0);

  tcsetattr(STDIN_FILENO, TCSANOW, &tty_stdin_restore);
  return 0;
  • Try running your program with stdin or stdout redirected, or both redirected but not to the same tty. Then try opening two fd's on the same disk file and see what happens if you write one and read the other. – dave_thompson_085 Jul 9 '17 at 3:47

You are changing attributes of the device, not the file descriptor. The file descriptor is just a way of identifying which device you're talking about. If both stdin and stdout are the same tty (/dev/pts/0 for example) then it doesn't matter which one you use for tcgetattr and tcsetattr.

Since echoing by definition involves input and output, it's hard to imagine what it might mean for echoing to be enabled on input and not output, or vice versa. Either the tty driver will echo, or it won't. Did you have a goal in mind that involves modifying the echo behavior in some way? If so, say what you're trying to accomplish and maybe someone will know how to do it properly.

| improve this answer | |
  • I'm trying to understand how to direct input from keyboard to serial port, and direct output from serial port to my monitor. I'm trying to understand exact path which the data is travelling. Also I do not understand from where comes "echo feedback loop" effect. (I have RX and TX of my com port connected directly) – Igor Liferenko Jul 9 '17 at 4:14
  • Do I have this path? terminal -> /dev/pts/0 -> program -> /dev/ttyS0 So, there are actually two TTYs in the path, not one? So, how do I know on which one of the two TTYs settings must be made (baud, echo, canonical, etc)? – Igor Liferenko Jul 9 '17 at 4:26
  • I don't know anything about what you are doing. The code you have shown didn't include opening any tty's, just operating on stdin and stdout, which are inherited from the parent process. There's no way I can tell where those file descriptors are pointing, and I also don't know what kind of serial device you have connected to your port. – user41515 Jul 9 '17 at 4:28
  • The "echo feedback loop" problem is described here unix.stackexchange.com/questions/42964, but I cannot understand the answer. – Igor Liferenko Jul 9 '17 at 4:28
  • You're just trying to repeat the experiment of cat /dev/ttyS0 and echo hi > /dev/ttyS0 with a looped-back port? It seems like disabling echo is a good idea then. Maybe if you post a question with all of your code, including the commands you're running to set things up, it would be possible to see what's going on. – user41515 Jul 9 '17 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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