From an answer to one my previous question I learned that shells (such as bash) have an ability not to follow the rules of terminal input processing set by stty(1). In particular, they can operate in raw mode while there is a setting enabled that turns on canonical mode (stty icanon) (with line discipline editing rules, and so on).

In this regard

  1. Is it correct to say that every running process (process group) can configure its own settings with respect to its terminal? In other words, there is no system-wide point of the settings of a tty instance, it is all individual to every single process (process group). (So bash explicitly sets up raw mode before starting to read a command name.)
  2. What does stty(1) exactly affect? My guess is this is a set of user preferences that is implemented by the terminal emulator with respect to the terminal used, which is a pty master side.
  3. When there are two sides communicating, bash on the slave one and the terminal emulator on master and they have set different tty configs (bash: "send me characters immediately, no line editing", the emulator: "send characters on EOL, line editing please"), why does the rules of bash win? What circumstances have an effect on such priority?
  4. If we run some cat command through bash it will obey the stty(1) settings. So does it mean bash explicitly defaults to these before executing the program or they are "inherited" to cat in some other way?

1 Answer 1


Any process can use tcsetattr to change the terminal driver characteristics (see man -s 3 termios for the complete 608-line story). The terminal itself only preserves the last state it saw -- it keeps no other history.

The polite usage is to nest any changes: any process that changes them should first read and save the current set, alter only those it wants to, and restore the original set it started with before it exits (including signal handlers for any terminations that it can).

The switch from line-at-a-time (canonical input) to single-character is a one-bit change in the c_lflag member of the termios structure, and there is a definition ICANON for the bitmask required. Likewise, the ECHO or not is a one-bit flip.

  • All in all, I get the picture but there is another bit: is there a difference between how tty operates on master vs slave sides? For example, if we have the echo option set the line discipline would send echo for the master side only. If we have isig on then ^C as input from the master side would cause SIGINT to be sent to the slave side, but not vice versa. So does it all mean a program should be somewhat aware of the side it is standing on? Jan 30 at 11:17
  • I have no idea what you mean by master & slave in this context. A process may have a single tty attached to several file descriptors (stdin, stdout and stderr). A pipeline like a | b | c will have all 3 processes connected to stderr, a only to stdin, c only to stdout. Jan 30 at 20:55

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.