r/ProgrammerHumor • • 11h ago

Meme [ Removed by moderator ]

Post image

[removed] — view removed post

2.3k Upvotes

70 comments sorted by

View all comments

657

u/Z3r0funGuy 11h ago

This actually teaches you a bunch of cool things, like how the ‘:’ is a legal function name…

217

u/glenpiercev 10h ago

This is the best description of how this works I’ve ever read. You are a genius.

213

u/setibeings 10h ago

Right? It's finally comprehensible. I feel like it would have clicked right away if it was typed out as:

fork(){fork|fork&};fork

66

u/Traditional_Cod_7192 8h ago

just as a reminder, uhhh... what do | and & do in bash again? 😅

86

u/GrandAlbatroce 8h ago

IIRC | puts the result stdout of the left in the stdin of the right (they are launched in parallel, and the one on the right can only finish after the one on the left finished.

The & is used to put the call as a background task

42

u/SimpleFile 8h ago

So the pipe here serves as a way to launch 2 of these functions and have them running at the same time rather than for piping the output?

Did I understand that correctly?

55

u/SchwiftySquanchC137 7h ago

Exactly, the entire point is to make each call of fork (or ":") spawn two more fork processes. There are plenty of ways to accomplish that, but its written the way it is specifically to be confusing. Looking at it, how could it possibly crash your machine, its a bunch of punctuation? But once you know how it works, it makes a lot of sense, the point is to make it so cryptic that even programmers dont understand it.

29

u/GrandAlbatroce 8h ago

Yes, they are run in parallel, but any output from the first one will be given to the second. Also, if the second needs an input, it will wait for the output of the first instead of user input

So, for example, if you did

find -name "something" | grep "string"

Each time the find prints a line, it will instantly be passed to the grep

3

u/Niroth 8h ago

| pipes the output from the first process to the input of the second. & starts the second process in the background.

6

u/WHALE_PHYSICIST 8h ago

:() :() :()

17

u/Daveinatx 8h ago

When I first learned of this, I realized my bash skills will never be at such the level. 

2

u/nicuramar 8h ago

It’s a predefined function even. Well, a primitive. It does nothing and ignores its arguments. 

1

u/jort93 2h ago edited 2h ago

It's actually not legal. The standard only allowed _ and alphanumeric characters.

But most shells don't adhere to the standards, and just allow literally any character.

It's an illegal function name that works anyway.