3

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?

peterh
  • 9,731
Curcuma_
  • 143
  • 5

1 Answers1

8

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.

Thomas Dickey
  • 76,765
  • A terminal emulator simply sees a series of events. Typed keys or pasted text look the same to the terminal emulator this answered my question. not only, I ignored any difference between a real and an emulator terminal. thank you for pointing GUI browsers issue. – Curcuma_ Mar 15 '18 at 08:43
  • 4
    But you did add the stripping of control characters in 292 (http://invisible-island.net/xterm/xterm.log.html#xterm_292), presumably to guard against those broken applications like browsers that add them there in the first place (note that others like gvim or anything that sets the selections to any arbitrary data do as well), but why not BS (nor DEL)? What's the use case? – Stéphane Chazelas Mar 15 '18 at 14:46