22

If I do something like:

$ cat /bin/ls

into my terminal, I understand why I see a bunch of binary data, representing the ls executable. But afterwards, when I get my prompt back, my own keystrokes look crazy. I type "a" and I get a weird diagonal line. I type "b" and I get a degree symbol.

Why does this happen?

raldi
  • 21,344
  • 33
  • 76
  • 86
  • 2
    [This link](http://bbs.archlinux.org/viewtopic.php?pid=423358#p423358) has the best answer I've seen so far. – raldi Sep 23 '08 at 21:22

6 Answers6

45

Because somewhere in your binary data were some control sequences that your terminal interpreted as requests to, for example, change the character set used to draw. You can restore everything to normal like so:

reset
Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
13

Just do a copy-paste:

echo -e '\017'

to your bash and characters will return to normal. If you don't run bash, try the following keystrokes:

<Ctrl-V><Ctrl-O><Enter>

and hopefully your terminal's status will return to normal when it complains that it can't find either a <Ctrl-V><Ctrl-O> or a <Ctrl-O> command to run.

<Ctrl-N>, or character 14 —when sent to your terminal— orders to switch to a special graphics mode, where letters and numbers are replaced with symbols. <Ctrl-O>, or character 15, restores things back to normal.

tzot
  • 92,761
  • 29
  • 141
  • 204
  • can you explain for what that special graphics mode is? – Xanlantos Apr 17 '18 at 11:17
  • 1
    Any ANSI terminal emulator has to emulate the old ANSI terminals; those terminals did have this graphics mode for crude graphics, so the emulators emulate this graphics mode too. (Or was it just the “crude graphics” part I should have mentioned? I thought it was too obvious.) – tzot Apr 17 '18 at 15:48
4

The terminal will try to interpret the binary data thrown at it as control codes, and garble itself up in the process, so you need to sanitize your tty.

Run:

stty sane

And things should be back to normal. Even if the command looks garbled as you type it, the actual characters are being stored correctly, and when you press return the command will be invoked.

You can find more information about the stty command here.

dsm
  • 10,263
  • 1
  • 38
  • 72
3

You're getting some control characters piped into the shell that are telling the shell to alter its behavior and print things differently.

Steve g
  • 2,471
  • 17
  • 16
2

VT100 is pretty much the standard command set used for terminal windows, but there are a lot of extensions. Some control character set used, keyboard mapping, etc.

When you send a lot of binary characters to such a terminal, a lot of settings change. Some terminals have options to 'clear' the settings back to default, but in general they simply weren't made for binary data.

VT100 and its successors are what allow Linux to print in color text (such as colored ls listings) in a simple terminal program.

-Adam

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
-1

If you really must dump binary data to your terminal, you'd have much better luck if you pipe it to a pager like less, which will display it in a slightly more readable format. (You may also be interested in strings and od, both can be useful if you're fiddling around with binary files.)

Dan
  • 61,568
  • 9
  • 61
  • 78