cat file.txt VS. cat < file.txt

Asked

Viewed 118 times

0

Personal hail,

I recently started studying bash Unix and I have a doubt that I can not resolve regarding the command "cat".

During the course of Code Academy, it presents the code in two moments:

cat file.txt

cat < file.txt

After much searching, I saw some websites and explanations that say the second example does not pass the file for reading. It passes the contents of the file as input pattern (Standard Input).

I am not getting to understand what would be the usefulness of this command using the operator "<".

How you use/would use this command "cat < file.txt"?

Links I used: https://www.tecmint.com/13-basic-cat-command-examples-in-linux/ https://unix.stackexchange.com/questions/258931/difference-between-cat-and-cat

1 answer

3


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:

  1. First case cat file.txt reads the file and sends it to stdout
  2. 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:

resultado

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)

  • 1

    Now I understood straight the thing of stdin and stdout. Thank you very much for the help William.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.