Linux Terminal – Why Does Linux Allow Backspaces When Pasting Text into a Shell

Securityshellterminalterminal-emulator

In many Linux distributions I come across, backspace characters are included when pasting text into a shell. Why is pasting of these characters not prevented – or does this feature provide some useful functionality?

In other words, does the Linux shell “know” if it’s a pasted string – or typed by hand?

How is this behaviour handled?

Best Answer

A shell is just an application running in a terminal. For pasting, only emulators relevant, but there are still "real" terminals (hint: the Linux console is not one of those).

Disregarding the various console implementations, because pasting text is done in a more limited manner, the terminals running in X are the point of the question. A terminal emulator simply sees a series of events. Typed keys or pasted text look the same to the terminal emulator.

Considering just terminal emulators (and select/paste between those), backspace is not a problem because select/paste work with what's displayed on the terminal's window. That is, if a user selects text on a terminal's window, only printable text (with possibly tab characters as a special case). There aren't any backspace characters (unless someone's got a buggy terminal implementation), because a backspace tells the terminal to move the cursor left. There's no printable reside left for the terminal to provide in a selection. There are hundreds of other terminal controls which might be used, but backspace is simple and widely used.

Backspace is a problem with poorly implemented applications such as browsers (which really should provide displays of printable text...), that apparently will store whatever some script-writer decides should be stored on the screen.

So... rather than ask why terminal emulators still allow BS, one might ask why GUI browsers allow this behavior.

Related Question