This single line of bash code will crash your system:
:(){ : | : &};:
It is a so-called fork bomb, duplicating itself with each call, eventually draining system resources dry.
Here is how it works, character by character.
:(){ : | : &};:
It is a so-called fork bomb, duplicating itself with each call, eventually draining system resources dry.
Here is how it works, character by character.
In bash, functions are defined with the syntax
foo() {
# function body
}
Using this syntax, the fork bomb first defines the function `:`, then calls itself.
foo() {
# function body
}
Using this syntax, the fork bomb first defines the function `:`, then calls itself.
Thus, we have
:() {
: | : &
}
What is happening inside the function? There are two symbols here that need explaining: `|` and `&`.
:() {
: | : &
}
What is happening inside the function? There are two symbols here that need explaining: `|` and `&`.
`|` is called the pipe symbol, enabling us to pass a function's output into another function's input.
For instance, `ls - l` outputs the contents of the current directory, line by line.
On the other hand, `grep xy` returns the lines from a text where "xy" is found.
For instance, `ls - l` outputs the contents of the current directory, line by line.
On the other hand, `grep xy` returns the lines from a text where "xy" is found.
Thus,
ls -l | grep xy
yields all lines from `ls -l` for which "xy" is a substring. This is called piping.
ls -l | grep xy
yields all lines from `ls -l` for which "xy" is a substring. This is called piping.
What about the ampersand symbol `&`? The command
foo &
launches the function `foo` as a background job, returning the control to the caller.
Let's put all of this together!
foo &
launches the function `foo` as a background job, returning the control to the caller.
Let's put all of this together!
The function
:() {
: | : &
}
first calls itself, then pipes the result into itself as a background process. Thus, the process keeps on forking itself, hence the name fork bomb.
As a result of this exponential growth, the system quickly runs out of its resources.
:() {
: | : &
}
first calls itself, then pipes the result into itself as a background process. Thus, the process keeps on forking itself, hence the name fork bomb.
As a result of this exponential growth, the system quickly runs out of its resources.
Overall, with a bit more user-friendly notation, this is the fork bomb:
fork() {
fork | fork &
}
fork
You might want to save all progress before trying this at home.
fork() {
fork | fork &
}
fork
You might want to save all progress before trying this at home.
I usually talk about math here, but I am a curious person with lots of interests.
If you have enjoyed this little thread, share it with your friends and give me a follow! I regularly post deep-dive explainers such as this.
If you have enjoyed this little thread, share it with your friends and give me a follow! I regularly post deep-dive explainers such as this.
Loading suggestions...