Bash Fork Bomb

Warning: DON’T run this code, unless you understand what it’s going to do.

This is bash fork bomb:


If you run it, it will keep forking a process and stuck you operating system. You’ll most likely have to reboot to get your computer back }:-D

Why does it work?
Well, the trick is in the colon “:”. A colon is a valid name of a bash function. So the above code is equivalent to this:

bomb() {bomb | bomb&}; bomb

and after indentation:

bomb() {
bomb | bomb&;

It’s not easier to see what is happening here:
We defined a bash function called “bomb”. It forks itself recursively, and sends one of its instances to the background.

Why run in the background?
If we run the recursive call in the foreground, the calling function will not complete until the call returns.
Since there is no stop condition for this recursion, the calling function would wait forever, and we’ll have only a single instance of the function running. Killing it would be as easy as ctrl+C.
On the other hand, when running the recursive call in the background, the calling function will complete immediately, and the recursion can continue.
(It’s almost impossible to kill all these instances of the bomb even with the kill or killall commands, because the number of processes will grow exponentially and will utilize the whole CPU in a very short time. Leaving us with the only option: Reboot)

Leave a Reply