0

Scenario:

$ cat t0.txt
xxx

$ grep xxx t0.txt > t0.txt
grep: t0.txt: input file is also the output

# exit status 2

$ cat t0.txt
<nothing>

Question: if input file is also the output AND the exist status is 2 (an error occurred), then why clearing the input file?

pmor
  • 509
  • 5
  • 11

1 Answers1

2

shell redirections happens first; so at first shell empty your output file t0.txt then it passes to the grep and then grep complains input & output are the same; how grep knows they are same(?) because it checks the inode number of the input &output file (see the source code here/lines 1377~1403).

unfortunately you lost your t0.txt content as the result.

αғsнιη
  • 40,939
  • 15
  • 71
  • 114