I am wondering how the Linux cat
decode a file. Suppose it is an English text file, the command seems to decode with ASCII, but how would cat
decode a random piece of file?
Asked
Active
Viewed 1,003 times
1 Answers
5
It absolutely does not decode anything. It simply reads a line at a time and outputs it, byte for byte.
To the extent that there is decoding going on, it happens within your terminal software. You probably want to read up on how your locale
affects the system's operation.
For a brief experiment, print something which isn't compatible with your terminal:
bash$ perl -e 'print("\xff\xff")'
��
Now observe how cat
affects it (i.e. not at all)
bash$ perl -e 'print("\xff\xff")' | cat
��
To examine in more detail what goes on, perhaps add a pipe to a hex dump program, like xxd
:
bash$ perl -e 'print("\xff\xff")' | xxd
00000000: ffff
bash$ perl -e 'print("\xff\xff")' | cat | xxd
00000000: ffff
(This assumes that you have a locale where the output is not a valid byte sequence for actual characters, like UTF-8.)
Perhaps see also What is character encoding and why should I bother with it

tripleee
- 175,061
- 34
- 275
- 318