Shell – What allows a command to be typed while the previous command is still running in foreground

command lineshellterminal

Sometimes I have seen when one program (say A) is running in the foreground and printing its output to stdout, I can type another command (say B), and when I press Enter it is run, even though I had not been prompted to type B since A had not finished executing yet. I could do this in the tcsh shell and the end result was that B was executed after A.

What is this feature called? Is this shell specific? How does it work without me typing the command at the prompt?

Best Answer

This is called typeahead, and it's not shell specific. What you type ends up being buffered in the terminal, and the next time a program running in the terminal is ready for input it reads what is waiting in the buffer. In your example that program is the shell, so it executes the command you typed as if you'd waited for A to finish before typing it.

Some programs will explicitly clear the buffer before accepting input; for example most programs which ask for a password will clear the buffer, to make sure the user knows what was typed (since the input isn't echoed).

Related Question