If I understand the cat manual correctly:

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 values 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?
  • Should a mechanism to prevent this behavior like try{}catch{} statement should be implemented?
  • 2
    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, 2014 at 10:30
  • 4
    a reset command might help sometimes, but this is no miracle solution.
    – Ouki
    Mar 13, 2014 at 10:47
  • The actual sequence to type is Control-J reset Control-J. Almost always restores sanity.
    – Joshua
    Jan 1, 2015 at 18:02
  • 1
    @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) Jan 2, 2015 at 1:56
  • 2
    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, 2015 at 3:37

2 Answers 2


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.

  • 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, 2014 at 10:12
  • 2
    @Kiwy control characters don't exist on your keyboard, but you can make echo output them if you choose. See stackoverflow.com/questions/5947742/… for how to do it and termsys.demon.co.uk/vtansi.htm for some of what is possible Mar 13, 2014 at 12:32
  • @DavidWilkins hey thanks that's nice, so much things to learn and no time for that :-(
    – Kiwy
    Mar 13, 2014 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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