Bash – Is It Safe to Type Another Command into STDIN While Writing to STDOUT

bashshellstdinstdout

Perhaps this has been answered previously, I would welcome a link to another answer…

If I execute a shell command (in a bash shell) like the following:

make

Then while the output from make is scrolling by from the STDOUT of the make command, if I type make check and press enter before the first command is finished executing, when the make command finally finishes the next command make check will pick right up and run.

My question(s) are simply:

  1. Is this dangerous to do?
  2. Are there any potential unexpected behaviors from this kind of rush typing?
  3. Why does this work the way it does?

Best Answer

It works the way it does because Unix is full-duplex. As Ritchie said in The UNIX Time-sharing System: A Retrospective:

One thing that seems trivial, yet makes a surprising difference once one is used to it, is full-duplex terminal I/O together with read-ahead. Even though programs generally communicate with the user in terms of lines, rather than single characters, full-duplex terminal I/O means that the user can type at any time, even if the system is typing back, without fear of losing or garbling characters. With read-ahead, one need not wait for a response to every line. A good typist entering a document becomes incredibly frustrated at having to pause before starting each new line; for anyone who knows what he wants to say any slowness in response becomes psychologically magnified if the information must be entered bit-by-bit instead of at full speed.

[end quote]

That being said, there are some modern programs that eat up or discard any typeahead; ssh and apt-get are two examples. If you type ahead while they're running, you may find that the first part of your input has disappeared. That could conceivably be a problem.

ssh remotehost do something that takes 20 seconds
mail bob
Dan has retired. Feel free to save any important files and then do
# ssh exits here, discarding the previous 2 lines
rm -fr *
.
Related Question