How can a bash script execute even after encountering a statement to delete itself?
For eg when I ran test.sh script which conains:
<--some commands-->
rm test.sh
<--some more commands-->
end
The script executes till the end before deleting itself
What actually happens is that bash keeps the file open and
rmwon’t make that stop.So
rmcalls 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 withls -i).The inode exists for as long as programs have it open.
You can easily test this claim as follows:
while in another window:
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.