As other people explained, the problem is that the I/O redirection is done before the sort
command is executed, so the file is truncated before sort
gets a chance to read it. If you think for a bit, the reason why is obvious - the shell handles the I/O redirection, and must do that before running the command.
The sort
command has 'always' (since at least Version 7 UNIX) supported a -o
option to make it safe to output to one of the input files:
sort -o file1 file1 file2 file3
The trick with tee
depends on timing and luck (and probably a small data file). If you had a megabyte or larger file, I expect it would be clobbered, at least in part, by the tee
command. That is, if the file is large enough, the tee
command would open the file for output and truncate it before sort
finished reading it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…