How does this bash fork bomb work?

BashUnix

Bash Problem Overview


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

:(){ :|:& };:

How does it work?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLajos NagyView Question on Stackoverflow
Solution 1 - BashJohn FeminellaView Answer on Stackoverflow
Solution 2 - BashTalljoeView Answer on Stackoverflow