Question

Suppose I have a file named "file1". I want to display the contents of "file1" using the cat command in Unix.

Both cat file1 and cat < file1 are working similarly. What is the difference between them?

Was it helpful?

Solution

It's where input comes from.

  • If you say cat file1 the shell doesn't do anything special. cat calls open(2) on the file and reads from it
  • If you say cat < file1 the shell calls open(2) on the file and calls dup(2) into STDIN_FILENO for cat. cat just reads from STDIN_FILENO

OTHER TIPS

We can use another command to notice the difference between:

wc –w food2.txt 

Possible output:

6 food2.txt 

the command tells the file name since it knows it (passed as an argument) .

wc –w < food2.txt 

Possible output:

6 

the standard input is redirected to file food2.txt without the command knowing about it .

cat opens a file, and cat > fileName tells the shell to open the file in the cat standard input.

Here is a link with more detailed information/answer: https://unix.stackexchange.com/questions/258931/difference-between-cat-and-cat

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top