0

Situation: When using the following command:

cat foo | sort | tee foo

where foo is a text file of multiple lines, the outcome is not consistent. The two outcomes I've observed are:

  1. The file is sorted, and the sorted contents are printed to stdout.
  2. The file is emptied, and nothing is printed.

Question: Why does this happen?

P.S. I realize that sort has a -o option on my system.

1 Answers1

1

You create a race condition. You start two commands (on both sides of the pipeline) which both try to access the same file in different ways (read vs. erase). The result is chance.

cat foo | sort | ( tee foo.new; mv foo.new foo)
Hauke Laging
  • 90,279