If you create an executable file with the following contents, and run it, it will delete itself.
How does this work?

  • 7
    Related Commented May 3, 2016 at 18:00
  • @DigitalTrauma Heh, that was my first thought when I saw this.
    – cat
    Commented May 3, 2016 at 23:16
  • it's not about rm, it is about the #!. The question could be rephrased to how does any executable script with a #! works.
    – njzk2
    Commented May 4, 2016 at 16:12
  • 4
    How did you manage to stumble upon this? Commented May 4, 2016 at 19:33
  • 1
    @user1717828 Probably PPCG.
    – wizzwizz4
    Commented May 5, 2016 at 6:06

1 Answer 1


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

/bin/rm scriptname

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 #!/bin/bash.

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.

  • Reminds me of ol' CrunchBang
    – Xen2050
    Commented May 3, 2016 at 23:53
  • "passing in the script's name" -- not quite. That's how it worked in the 70s, but it invites a race condition hack. You can start a script with one heading, or set of file permissions, then change it before the interpreter is started. Instead, I believe such files are opened and read by the OS, then LEFT opened and passed to the interpreter as file handle 2 (or 0?). I'm not 1000% sure of this so I won't edit the actual answer Commented Mar 7, 2020 at 4:15
  • 5
    @SwissFrank yes, there is a race, and yes, there are loaders (in the Linux kernel) which support passing an open file descriptor, but not for scripts. In fact, if the race were fixed for scripts, arbitrary commands would no longer work as shebangs, and rm couldn’t be used in this way. Commented Mar 7, 2020 at 7:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .