If you create an executable file with the following contents, and run it, it will delete itself.
How does this work?
The kernel interprets the line starting with
#! and uses it to run the script, passing in the script’s name; so this ends up running
which deletes the script. (As Stéphane Chazelas points out,
scriptname here is sufficient to find the script — if you specified a relative or absolute path, that’s passed in as-is, otherwise whatever path was found in
PATH is prepended, including possibly the emptry string if your
PATH contains that and the script is in the current directory. You can play around with an echo script —
#!/bin/echo — to see how this works.)
As hobbs pointed out, this means your script is actually an
rm script, not a
bash script — the latter would start with
See How programs get run for details of how this works in Linux; the comments on that article give details for other platforms.
#! is called a shebang, you’ll find lots of information by searching for that term (thanks to Aaron for the suggestion). As jlp pointed out, you’ll also find it referred to as “pound bang” or “hash bang” (
# is commonly known as “pound” — in countries that don’t use
£ — or “hash”, and
! as “bang”). Wikipedia has more info.