raldi
raldi

Reputation: 22170

Why do my keystrokes turn into crazy characters after I dump a bunch of binary data into my terminal?

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?

Upvotes: 22

Views: 9687

Answers (6)

tzot
tzot

Reputation: 96061

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.

Upvotes: 14

dsm
dsm

Reputation: 10395

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.

Upvotes: 4

Dan
Dan

Reputation: 63460

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.)

Upvotes: -1

Adam Davis
Adam Davis

Reputation: 93625

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

Upvotes: 2

Nick Johnson
Nick Johnson

Reputation: 101149

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

Upvotes: 45

Steve g
Steve g

Reputation: 2499

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

Upvotes: 3

Related Questions