Why (and how) did using cat on binary files mess up the terminal?

20,695

Solution 1

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

but,

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.

Solution 2

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:
20,695

Related videos on Youtube

S edwards
Author by

S edwards

Nice person willing to save the Universe...

Updated on September 18, 2022

Comments

  • S edwards
    S edwards over 1 year

    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?
    • Admin
      Admin about 10 years
      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.
    • Admin
      Admin about 10 years
      a reset command might help sometimes, but this is no miracle solution.
    • Admin
      Admin over 9 years
      @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)
    • Admin
      Admin over 9 years
      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.
  • S edwards
    S edwards about 10 years
    does it means that if you cat a binary files that may contains in plain text instruction like rm -rf . this could be interpreted ?
  • David Wilkins
    David Wilkins about 10 years
    @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
  • S edwards
    S edwards about 10 years
    @DavidWilkins hey thanks that's nice, so much things to learn and no time for that :-(