In unix, a file isn't truly deleted until the last directory entry for it is removed (e.g. with rm
) and the last open file handle for it is closed. See this question (especially MarkR's answer) for more info. In the case of your script, the file is opened as stdin for the while read
loop, and until that loop exits (or closes its stdin), rm
ing the file will not actually delete it off disk.
You can see this effect pretty easily if you want. Open three terminal windows. In the first, run the command cat >/tmp/deleteme
. In the second, run tail -f /tmp/deleteme
. In the third, after running the other two commands, run rm /tmp/deleteme
. At this point, the file has been unlinked, but both the cat
and tail
processes have open file handles for it, so it hasn't actually been deleted. You can prove this by typing into the first terminal window (running cat
), and every time your hit return, tail
will see the new line added to the file and display it in the second window.
The file will not actually be deleted until you end those two commands (Control-D will end cat
, but you need Control-C to kill tail
).