Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

#!/bin/rm
share|improve this question
79  
It's not a bash script, it's a rm script. – hobbs yesterday
4  
Related – Digital Trauma yesterday
    
@DigitalTrauma Heh, that was my first thought when I saw this. – cat yesterday
    
it's not about rm, it is about the #!. The question could be rephrased to how does any executable script with a #! works. – njzk2 7 hours ago
    
How did you manage to stumble upon this? – user1717828 4 hours ago
up vote 80 down vote accepted

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 in a comment, 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.

share|improve this answer
8  
Other names for "#!" you might hear are "pound bang" and "hash bang". See en.wikipedia.org/wiki/Shebang_(Unix) for details. – jlp yesterday
    
@jlp Pound bang? Is that like "bang for your buck"? Heh... – cat yesterday
    
Reminds me of ol' CrunchBang – Xen2050 23 hours ago
    
Please consider adding hobbs's observation from the comments; it would make this answer complete. That the OP is not running a Bash script is (in my view) a critical piece of understanding here. I realise that you touch on it, but it could be made more clear. – Lightness Races in Orbit 11 hours ago
    
@LightnessRacesinOrbit thanks for the nudge, I've just merged hobbs' and jlp's comments. – Stephen Kitt 11 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.