126

According to Wikipedia, the following is a very elegant bash fork bomb:

:(){ :|:& };:

How does it work?

6

2 Answers 2

167

Breaking it down, there are three big pieces:

:()      # Defines a function, ":". It takes no arguments.
{ ... }; # The body of the function.
:        # Invoke the function ":" that was just defined.

Inside the body, the function is invoked twice and the pipeline is backgrounded; each successive invocation on the processes spawns even more calls to ":". This leads rapidly to an explosive consumption in system resources, grinding things to a halt.

Note that invoking it once, infinitely recursing, wouldn't be good enough, since that would just lead to a stack overflow on the original process, which is messy but can be dealt with.

A more human-friendly version looks like this:

kablammo() {             # Declaration
  kablammo | kablammo&   # The problematic body.
}; kablammo              # End function definition; invoke function.

Edit: William's comment below was a better wording of what I said above, so I've edited to incorporate that suggestion.

7
  • 9
    ...and, kablammo!
    – S.P
    Commented Jun 13, 2009 at 17:50
  • 2
    Just make sure you stand clear of the blast when your system goes up in smoke! Commented Jun 24, 2009 at 2:01
  • 9
    Minor nit: "the function is invoked twice and an instance is backgrounded" is not quite right. The function is invoked twice in a pipeline and the pipe is backgrounded, so both instances are being run in the background. Although that's a semantic detail as in reality it is likely that only one is ever attempted, and its recursion halts the system before the other ever starts. Commented Aug 28, 2009 at 12:03
  • 4
    @william: "its recursion halts the system before the other ever starts", so does that mean that :(){ :& };: would work as a bomb too?
    – Dan
    Commented Nov 16, 2009 at 5:22
  • 7
    @Dan: Unlikely, because it would terminate immediately after creating its child process. The big ingredient you need is that the fork bomb not terminate so that the total number of processes grows over time rather than holding steady. Commented Apr 29, 2011 at 19:09
9

Short answer:

The colon (":") becomes a function, so you are running the function piped to the function and putting it in the backgroun which means for every invocation of the function 2 copies of the function are invoked. Recursion takes hold.

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