Alana Storm
Alana Storm

Reputation: 166166

Why Does Piping Binary Text to the Screen often Horck a Terminal

Imaginary Situation: You’ve used mysqldump to create a backup of a mysql database. This database has columns that are blobs. That means your “text” dump files contains both strings and binary data (binary data stored as strings?)

If you cat this file to the screen

$ cat dump.mysql

you’ll often get unexpected results. The terminal will start beeping, and then the output finishes scrolling by you’ll often have garbage chacters entered on your terminal as through you’d typed them, and sometimes your prompts and anything you type will be garbage characters.

Why does this happen? Put another way, I think I’m looking for an overview of what’s actually happening when you store binary strings into a file, and when you cat those files, and when the results of the cat are reported to the terminal, and any other steps I’m missing.

Upvotes: 5

Views: 295

Answers (3)

amertune
amertune

Reputation: 690

When you cat the binary data to the screen, the terminal tries to interpret that binary data into ASCII (or UTF). Some characters are capable of controlling the terminal. For example,

echo "^[[0;31;40m" # The first ^[ comes from pressing Ctrl+v, Esc

Will turn the background black and the foreground red. Use reset to return your terminal to normal.

Upvotes: 2

Byron Whitlock
Byron Whitlock

Reputation: 53921

When you cat a binary file you can inadvertently send control characters to the terminal.

If a terminal application wants to send a beep for example, it sends the following binary to the terminal: 0x007 (SYS V only).

The same goes for colors, cursor position and others.

Upvotes: 6

Daniel Martin
Daniel Martin

Reputation: 23568

Start here: http://www.faqs.org/docs/Linux-HOWTO/Keyboard-and-Console-HOWTO.html

In particular, sections 3 (Console generalities) and section 4 (reseting your terminal).

It covers a bit more than you're talking about, but should give you what you need.

Upvotes: 5

Related Questions