[ACCEPTED]-How does this bash fork bomb work?-unix

Accepted answer
Score: 166

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 14 the body, the function is invoked twice 13 and the pipeline is backgrounded; each successive 12 invocation on the processes spawns even 11 more calls to ":". This leads rapidly to 10 an explosive consumption in system resources, grinding 9 things to a halt.

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

A more human-friendly 4 version looks like this:

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

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

Score: 9

Short answer:

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

More Related questions