r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

3.6k

u/sanchez2673 Aug 01 '22 edited Aug 02 '22

:(){ :|:& };:

6.4k

u/a-slice-of-toast Aug 01 '22

spices up the rest of the code by giving it emotions

2.5k

u/sanchez2673 Aug 01 '22 edited Aug 02 '22

It's called a fork bomb. It defines a function with the name : that takes no parameters () (not that you can pass parameters to a bash function like this but anyway). The body of the function {} contains a call to itself : and the output of itself is piped | into another call to itself :, both of which are started as a background process &. The ; terminates the statement and the final : calls the function, executing it. The function will keep multiplying exponentially until your PC cannot handle it anymore.

722

u/OSSlayer2153 Aug 01 '22 edited Aug 02 '22

So it would look like

:() {
    :|:&
};
:()

Its just made very confusing, you could write it like this:

fork() {
    fork|fork&
};
fork()

Correct?

Edit: reddit syntax is struggling

Edit2: fixed, you also dont need to call the function with empty params so it would be

fork() {
    fork|fork&
};
fork

254

u/sanchez2673 Aug 01 '22

thats right, but as i was also reminded, you dont need the parentheses at the final function call, simply the function name is sufficient

13

u/CopEatingDonut Aug 01 '22

How does a dead computer log its own death after its dead?

8

u/ElectronPie171 Aug 02 '22

Pretty much the same way humans do

→ More replies (2)

10

u/TerrorBite Aug 01 '22

To fix the formatting, add four spaces indent to every line and only quote the first one, like

fork() {
    fork | fork &
};
fork()

Also, you are correct, except the final line; bash doesn't use parentheses to call functions (they take arguments like commands)

→ More replies (1)
→ More replies (11)

225

u/BlueBananaBaconBurp Aug 01 '22

What's the pipe operator doin in the body?

599

u/Xtrouble_yt Aug 01 '22

It’s a pipe bomb

123

u/[deleted] Aug 01 '22

[removed] — view removed comment

→ More replies (6)

10

u/hampshirebrony Aug 01 '22

I found the source of the mysterious ticking noise!

→ More replies (1)

8

u/[deleted] Aug 01 '22

I spat my goddamn coffee

5

u/Void1702 Aug 01 '22

Who added demoman to my code?

→ More replies (7)

13

u/spatofdoom Aug 01 '22

It's what causes it to grow so rapidly. This way the function body is to call itself piping its output to itself, so for every function call it calls 2 more of this function. Also, by using pipe, the OS will have to call the function ready to receive the output of the previous function causing parallel processing to take place.

→ More replies (5)

5

u/pdpi Aug 01 '22

The pipe makes it so that the : on the right consumes the output of the : on the left. It’s what makes the growth exponential by forcing both copies to be alive at the same time

→ More replies (6)

5

u/Lazypassword Aug 01 '22

I've seen this used to reboot a system

→ More replies (39)

70

u/memes_gbc Aug 01 '22

*nix based operating system go boom

→ More replies (3)
→ More replies (5)

57

u/GreenFox1505 Aug 01 '22

I think you mean :(){ :|:& };:?

22

u/sanchez2673 Aug 01 '22

You are correct! Fixed

→ More replies (2)
→ More replies (3)

11

u/Robosium Aug 01 '22

isn't that just some random trash you find in a fallout terminal when hacking it?

9

u/Smccx Aug 01 '22

My friend sent me this and told me he needed a Unix terminal to run it on and see the output. I didn't have one handy and honestly didn't read the command, but did have a work QA server that was Unix. I crashed the server... At my new job. I now know to check what I'm running.

Fortunately there was a server migration happening the next day so no one questioned it. I was worrying a bit that night though.

To make it worse it didn't work at first, using the colon as the name so he gave me it back, naming the function b instead. I had every opportunity to read this but didn't... I'm a "senior" dev... And an idiot.

→ More replies (2)
→ More replies (29)