Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

bash - How does cat interpret a file when it initially reads it?

I have been researching into the usage of the cat command on a deeper level (long story) and I was wanting to clarify understanding.

When we cat a binary, let's say the cat binary itself: cat /usr/bin/cat we see that the terminal will slightly mess up as cat interprets these as escape sequences, this seems down to how cat interprets the bytes, but upon research, I did not find exactly how cat achieves this.

A further example would be a simple bash script, in which we can do cat bash_script.sh | bash and pipe it into bash to execute it as a bash script, this is the same for python etc.

So to summarise, I am wondering how cat interprets the bytes as it reads them, and then how they are printed to the screen.

Any help is welcome, thanks in advance :)

question from:https://stackoverflow.com/questions/66049010/how-does-cat-interpret-a-file-when-it-initially-reads-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

cat is not interpreting the bytes. Your terminal emulator (e.g. gnome-terminal) is interpreting the bytes.

You can find some of the codes in the console_codes man page. There may be some variation from terminal to terminal in exactly what codes they support.

You can also check out this blog post for a description of how to use the codes to achieve various effects such as colored text.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...