What actually happens is that bash keeps the file open and rm
won't make that stop.
So rm
calls the libc function "unlink()" which will remove the "link" to the inode from the directory it's in. This "link" is in fact a filename together with an inode number (you can see inode numbers with ls -i
).
The inode exists for as long as programs have it open.
You can easily test this claim as follows:
$ echo read a> ni
$ bash ni
while in another window:
$ pgrep -lf bash\ ni
31662 bash ni
$ lsof -p 31662|grep ni
bash 31662 wmertens 255r REG 14,2 7 12074052 /Users/wmertens/ni
$ rm ni
$ lsof -p 31662|grep ni
bash 31662 wmertens 255r REG 14,2 7 12074052 /Users/wmertens/ni
The file is still opened even though you can no longer see it in ls.
So it's not that bash read the whole file - it's just not really gone until bash is done with it.
rm test.sh
still exist in memory to be executed. – Zirkle