The command cat file.txt
simply reads the file and displays it on the screen, or more precisely displays it on stdout
, not always going to be a screen, in your current case yes, since it is using terminal, but stdout
does not refer to the screen but to the output of a program, the terminal display the result is "consequence"
The operator <
reads the file (without needing the cat
) and plays it for "somewhere", ie passes the stdin
of the previous command, in the case when doing this cat < file.txt
the < file.txt
is already reading the file and passing the contents to the stdin
of cat
and the cat
in turn returns to the stdout
(which in your case will display directly on the screen)
To sum up then:
- First case
cat file.txt
reads the file and sends it to stdout
Next case cat < file.txt
The <
takes the contents of the file and adds to the stdin of the cat
and then the cat
as it did not receive parameter for reading a file but received the stdin
then return the own stdin
for the output
How you use/would use this command "cat < file.txt"?
Basically this command will serve no purpose, it’s serious, not in the way that this, this is just for you to understand the behavior of <
, since the cat
can work with parameters such as reading 2 files and displaying them in stdout
:
cat foo.txt bar.txt
When reading a stdin, for example if you pass any text in the stdin
of cat
it will display what you typed right after Enter or after Ctrl+D (2 times, I say in the terminal), do this (if the $ is just to illustrate):
$ cat
Then type:
Ola mundo!
And press Enter and it will be displayed directly what you were typing:
The first "hello world" was where I could type, the second is just displayed and I can’t directly manipulate it
That is, in practice these examples are only to understand the behavior of stdin
of each program, understand that the stdin
does not refer to the stdin
from the terminal, but from any program that will run, as each program has its own flow (stdin, stdout, etc)
The response of Unix & Linux was not enough to clear the doubt?
– Jéf Bueno