Reading & Writing to the same file from terminal

18 views Asked by At

Given a file foo

1
2
3
4
5

Can someone help me understand why the following results in an empty file?

cat foo | grep 1 > foo
wc foo
0       0       0 foo

whereas the following appends fine

cat foo | grep 1 >> foo
cat foo
1
2
3
4
5
1

I'm not surprised with the functioning of >>, but with > my expectation was that the file will contain just the output of cat foo | grep 1. What's the underlying implementation detail that results in this behaviour?

cat foo | tee foo

on the other hand does not empty the file.

0

There are 0 answers