3

Using xterm and bash 4.3 from the (pseudo-)terminal pts/10 we type (█ is the cursor):

test@debian:~$ ps
  PID TTY          TIME CMD
13128 pts/10   00:00:02 bash
21037 pts/10   00:00:00 ps
test@debian:~$ ls
Mail  directory  file
test@debian:~$ ls directory
file1  file2  file3
test@debian:~$ ls █

(as you see, there is a space after ls)

Then from another terminal we send a backspace:

test@debian:~$ echo -ne "\b" > /dev/pts/10

In terminal pts/10 we now have:

test@debian:~$ ls█

We type directory, so we have:

test@debian:~$ lsdirectory█

Pressing return we get "directory" listed:

file1  file2  file3
test@debian:~$ █

Of course, normally entering lsdirectory would give us a "command not found"-error. Somewhere the space has been saved. Where?

viuser
  • 2,614

1 Answers1

6

Your shell's command line editor saved the space. In fact, it saved all of the characters you typed into it that made up the command line.

I wonder if you might be under the impression that the shell knows what command to execute by reading back the contents of the screen just before executing the command. That's not the case. Moreover, terminals do not necessarily even provide any method of reading back the contents of the screen, so the shell could not count on being able to do that even if it wanted to.

Also: you should be careful with multiple applications using the same terminal at the same time. In this case, your shell is running its command line editor on your terminal and you echoed a space into the same terminal from somewhere else. Your shell has no idea you've done that and your shell's idea of the cursor position is now out of sync with where the cursor actually is on screen. This could confuse and corrupt your shell's ability to display the command line properly. If you edit the command line using arrow keys and menu-based tab completion and such, your shell will want to move the cursor around and re-display parts of the command line, but it makes mistakes because the cursor is not where it thinks it is.

Celada
  • 44,132