I get the general idea of how this common version
:(){ :|:& };:
of the bash fork bomb works.
However, I've seen another version (for bash in particular)
#!/bin/bash
$0 &
$0 &
on the Wikipedia fork bomb article and in an SO answer to a closed duplicate of the original fork bomb question I mentioned above.
I'm looking for an explanation of how the second (perhaps less common) version of the fork bomb works. I've commented the code below with my current understanding of what it does, but I don't really get how it achieves infinite recursion in the way that the other version of the bash fork bomb does (probably due to my poor understanding of bash processes and backgrounding).
#!/bin/bash # Specifies the location of the executable
# to use in executing this script.
$0 & # Duplicates (executes a new instance
# of) the current running process ('$0')
# (which would be the invocation of bash used
# to start running the script, right?)
# and background it ('&').
$0 & # Do the same as above, where $0 would be
# equivalent to the initial call to bash
# used to start the script, right? Or, would
# it be the backgrounded call to bash from the
# second line? I'm leaning towards the former.
EDIT: My revised understanding of the script (at least at the level of abstraction I'm worried about at the moment) is below in the form of commented code. I've left my original commented code up for future viewers who might have the same initial misunderstandings I had. Assume the script lives in bomb.sh
.
#!/bin/bash # Script will execute using /bin/bash executable.
$0 & # The current process (P) will spawn a child process (C1)
# by invoking the command that spawned P
# (/bin/bash ./bomb.sh). This makes the script recursive.
# & allows processes to run in the background
# (allowing process death and avoiding a potential
# process limit).
$0 & # Process P spawns a second child process (C2), also
# in the background, which gives us the exponential growth
# (in base 2) that we want per level of recursion.