简体   繁体   中英

Difference between cat filename and cat < filename in unix

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM