What will happen when I 'cat' 'ksh'?

1

I used the following command in my unix box.

$ cat ksh

And it gave a rolling log of latin and greek letters and symbols and it kept on going with no signs of stopping. Eventually I had to close my connection tool to stop it.

I was just wondering what was it displaying?

Deena

Posted 2012-05-11T18:24:46.563

Reputation:

You missed the command! – Andreas Florath – 2012-05-11T18:27:00.893

No, he failed at formatting. Fixed that. – larsks – 2012-05-11T18:29:05.423

1Dude, that's what happens when you "cat" a binary file :) – paulsm4 – 2012-05-11T18:29:35.593

If you're up for a real thrill, try to "type" an .exe in a Windows command prompt. You'll get the Latin and Greek letters, the symbols, and it will even beep at you, too! ;) – paulsm4 – 2012-05-11T18:31:20.850

Not sure that this is a programming question; perhaps superuser would make more sense? – Charles Duffy – 2012-05-11T18:33:26.823

You can also use the "strings" command to see the text in a file: cd /bin;strings ls|less – paulsm4 – 2012-05-11T19:50:48.097

Answers

4

If you were running cat on the actual ksh binary file, then you were seeing the raw machine code of the ksh program being interpreted as if it was human-readable text. Of course, it wasn't, so the characters displayed were random characters from your terminal's character set, plus control and other non-printable characters.

Some of the control characters in a binary file can mess up your terminal's state. If you close the connection, that's fine, but you can also recover without closing your connection by pressing control-C to stop the process and then typing "reset" and pressing enter.

Andrew G.

Posted 2012-05-11T18:24:46.563

Reputation: 176

1Addendum: the command hd $(which ksh) | less will display the same data (the contents of the ksh executable) in a format that humans can actually get useful information out of. – zwol – 2012-05-11T18:32:28.137

bash: hd: command not found – Stephen P – 2012-05-11T18:40:16.123

The bytes of ksh may be seen with od -tx1 ksh. – None – 2012-05-11T18:50:45.217

On my Linux box (Debian) hd comes from the bsdmainutils package. I don't know where to get it for other distributions. OSX doesn't have hd but it does have hexdump which does something similar (sadly, it lacks the ASCII column which hd provides). – zwol – 2012-05-12T07:14:25.770

Vim comes with xxd, and util-linux has hexdump -C. @Zack, does OSX hexdump support -C? – user1686 – 2012-05-16T12:39:56.440

It does! I didn't realize this because there is no manpage. – zwol – 2012-05-16T15:20:36.863

0

A binary executable is a file like any other; its contents aren't guaranteed to make sense to humans, and usually don't. cat won't stop you from displaying it, because it can't really tell the difference between an executable and (say) a text file with a lot of non-ISO-8859-1 Unicode in it.

geekosaur

Posted 2012-05-11T18:24:46.563

Reputation: 10 195

0

That is because you were trying to print the contents of a binary file. I have done that a few times before by accident, and it almost always crashes putty. Now, for some real fun, go ahead and do cat /dev/urandom: always a good time. (urandom makes an awesome random data generator for things such as generating passwords, coin flips, etc., plus it is "true" random data because this data is pulled from the entropy pool (which is based on things such as electrical noise, etc., that the server monitors).)

lacrosse1991

Posted 2012-05-11T18:24:46.563

Reputation: 215