Bash – How does bash retrieves what was written on the terminal (without Enter being pressed)

bashinputterminaltty

Say that the ping command is running, and I type something on the terminal while ping is still running.

Now when ping terminates and bash gain back control, bash will print on the terminal what I typed while ping was running. This is a screenshot that shows what I mean:

enter image description here

How did bash get this information? I am sure it did not get it from stdin, because when I typed "I typed this while ping was running", I did not press Enter (and so stdin is empty).

Best Answer

Bash did get your input from its standard input (stdin), which is the terminal.

A terminal can be in one of two modes: raw or cooked (also known as “character mode” and “line mode”, the terms “raw” and “cooked” are mostly used in the Unix world) (the word “cooked” was introduced as a pun to make an opposite to “raw”). In raw mode, the terminal transmits input immediately to the application. In cooked mode, the terminal reads a full line and provides a (very primitive) line edition mechanism, and only transmits the data to the application when the user presses Enter.

Terminals start in cooked mode. Applications that want more control over input, such as bash, set the terminal in raw mode while they're waiting for the user to type a command.

Related Question