Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

If I understand correctly the cat manual:

concatenate files and print on the standard output

cat will take files as argument and print them on standard output.
What I don't get is if I use the command

cat img.png > copy.png

I will obtain 2 png files identical while if I just

cat img.png  

I have all chance that my terminal get messed up and misinterpret what I type.

How's that possible ?
Binary value are still binary data why it does not simply shows a series of 0 and 1 or the interpretation of those binary data in ASCII or whatever the encoding in terminal is ?
Is this behavior also possible by cating a text file containing strange characters ?

Edit 1:

Should a mechanism to prevent this behavior like try{}catch{} statement should be implemented ?

share|improve this question
Your terminal does not get messed up. It is in a state you forced it into by sending it control characters. That you cannot use it any more after changing the state might not be what you wanted, but it is entirely a result of you not understanding the consequences of your actions. That would be the same as switching your font color to green in a word processor and saying your word processor is messed up, only because you don't know how to switch it back to black font without e.g. exiting the program. – Anthon Mar 13 '14 at 10:30
a reset command might help sometimes, but this is no miracle solution. – Ouki Mar 13 '14 at 10:47
The actual sequence to type is Control-J reset Control-J. Almost always restores sanity. – Joshua Jan 1 at 18:02
@Joshua And what's the difference between a lone reset and a reset between Ctrl-J keypresses? I can't see any (nor any reason to go the more complicated way) – syntaxerror Jan 2 at 1:56
Because if the terminal was left in RAW mode, Enter generates Ctrl-M instead of Ctrl-J so the shell doesn't see the necessary keystroke to end a line and run the command. – Joshua Jan 2 at 3:37

2 Answers 2

up vote 2 down vote accepted

cat concatenates files given as arguments on command line to standard output, it reads bytes at a time an as default does not perform any interpretation of the bytes it reads.

In your first example you are redirecting stdout to a file, that's why you get a new file.

In your second example the bytes are written to the terminal, and it is the terminal which is interpreting sequences of characters as control sequences for the terminal, this is why you get unusual behaviour on your terminal. It has nothing to do with cat as such, cat doesn't know what you are going to do with it's output. You might be sending it through a pipe to another program to interpret/process/print or play "Singing in the rain".

So following the unix philosophy,

do one thing, do one thing only, but do it well

cat should not attempt to second guess what you are trying to do.

edit 1 reply to @kiwy's 1st comment below.

Yes and No, let me explain,

No, if you cat to a terminal, because it (the terminal software) is sending the output to your screen or interpreting control sequences (it is emulating an old piece of hardware ie. a teletype device).


Yes if you cat to a pipe and the program recieving can interpret the characters as commands.

look cat this for an example, cat anyOldShellScript | bash bash will interpret what it gets as commands.

share|improve this answer
does it means that if you cat a binary files that may contains in plain text instruction like rm -rf . this could be interpreted ? – Kiwy Mar 13 '14 at 10:12
I accept the answer though I don't really get why terminal can get messed up like this will if I type like dumbass on my keyboard I never manage to get this :D – Kiwy Mar 13 '14 at 10:30
And now irony... hum – Kiwy Mar 13 '14 at 10:43
@Kiwy control characters don't exist on your keyboard, but you can make echo output them if you choose. See… for how to do it and for some of what is possible – David Wilkins Mar 13 '14 at 12:32
@DavidWilkins hey thanks that's nice, so much things to learn and no time for that :-( – Kiwy Mar 13 '14 at 12:33

I guess this happens mostly because of non-printable characters with codes below 0x20. Those are special control/escape codes, which are used for keys like Backspace, Delete etc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.