raw and cooked modes are descriptive terms. stty raw
uses different settings than bash.
Bash does terminal initialization in prepare_terminal_settings (an internal function of the readline library), setting the terminal mode to allow reading a single character at a time without echoing:
tiop->c_lflag &= ~(ICANON | ECHO);
however, the carriage-return translation is reset in a different part of the function:
/* Make sure we differentiate between CR and NL on input. */
tiop->c_iflag &= ~(ICRNL | INLCR);
If you compare prepare_terminal_settings
to the coreutils stty
, the latter does less (several points, but bash resets INLCR
as well):
else if (STREQ (info->name, "raw") || STREQ (info->name, "cooked"))
{
if ((info->name[0] == 'r' && reversed)
|| (info->name[0] == 'c' && !reversed))
{
/* Cooked mode. */
mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
mode->c_oflag |= OPOST;
mode->c_lflag |= ISIG | ICANON;
#if VMIN == VEOF
mode->c_cc[VEOF] = CEOF;
#endif
#if VTIME == VEOL
mode->c_cc[VEOL] = CEOL;
#endif
}
else
{
/* Raw mode. */
mode->c_iflag = 0;
mode->c_oflag &= ~OPOST;
mode->c_lflag &= ~(ISIG | ICANON
#ifdef XCASE
| XCASE
#endif
);
mode->c_cc[VMIN] = 1;
mode->c_cc[VTIME] = 0;
}
}
POSIX says of stty raw
:
Enable (disable) raw input and output. Raw mode shall be equivalent to setting:
stty cs8 erase ^- kill ^- intr ^- \
quit ^- eof ^- eol ^- -post -inpck
which interestingly enough (following the descriptions of -post
and -inpck
) does not address carriage return translation on input.
Since the terms raw and cooked (either POSIX or coreutils stty) do not correspond to what bash does, mentioning the POSIX termios features that correspond to what it actually does are the way to go: icrnl
(input carriage-return to newline translation).
stty -a
should give a complete list of what the terminal settings are. In particular, the settings forinlcr igncr icrnl
determine what the terminal does with CR-NL translation on input and the correspondingo
flags tell what happens on output. Thomas DIckey's answer below explains how bash sets those flags in detail.bash
, and "on output" mean whenbash
send something to the terminal?bash
sends something to the kerne/tty devicel?