10

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?
5
  • 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
    Commented Mar 13, 2014 at 10:30
  • 4
    a reset command might help sometimes, but this is no miracle solution.
    – Ouki
    Commented Mar 13, 2014 at 10:47
  • The actual sequence to type is Control-J reset Control-J. Almost always restores sanity.
    – Joshua
    Commented 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) Commented 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
    Commented Jan 2, 2015 at 3:37

2 Answers 2

9

cat concatenates file(s) given as argument(s) on the command line and prints the concatenated output to the standard output.  It reads bytes (conceptually, one-at-a-time) and, by default, does not perform any interpretation of the bytes it reads.

In your first example, you are redirecting stdout to a file, which is why you get a new file.

In your second example, the bytes are written to the terminal, and it is the terminal that 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 per secat doesn't know what you are going to do with its output. You might be sending it through a pipe to another program to interpret/process/print it or which passes it to an audio driver to 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 or interpret what you are trying to do.


From @Kiwy's comment below (fixed for grammar):

does this mean that if you cat a binary file which contains in plain-text instructions like rm -rf, this could be interpreted?

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, e.g., a teletype device).

but:

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

Look at this for example:

cat anyOldShellScript | bash

In this example, bash will interpret what it gets as commands, and therefore run the characters rm -rf if they exist in that content passed to the pipe by cat.

3
  • 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
    Commented Mar 13, 2014 at 10:12
  • 3
    @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 Commented Mar 13, 2014 at 12:32
  • 1
    @DavidWilkins hey thanks that's nice, so much things to learn and no time for that :-(
    – Kiwy
    Commented Mar 13, 2014 at 12:33
4

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .