1

I was reading some commands on explainshell.com and find an interesting command:

:(){ :|:& };:

Currently I'm under OSX and the only reference to it that I could find was on builtin man page (man builtin) but that didn't help because it only says whether it's an external, cshell or sh command.

       Command       External    csh(1)    sh(1)
       !             No          No        Yes
       %             No          Yes       No
       .             No          No        Yes
       :             No          Yes       Yes

So, what does executing : do?

On terminal I've got no output on it:

$ :
$
AndreDurao
  • 2,040
  • 2
  • 13
  • 8
  • 2
    In this case, this question isn't a duplicate as the : isn't being used as a built-in. – John Oct 26 '16 at 19:22

2 Answers2

10

In this case, : isn't a command, it's the name of a function that you're defining. The command :(){ :|:& };: is what is called a "forkbomb" - it's a command sequence that defines a function called :, tell the shell that function consists of calling :, piping the output to another instance of :, and backgrounding the second instance. The final ;: sequence terminates the definition of the function and then calls it. Each call to : results in two more copies of : being run, as fast as the shell can spawn them.
This will result in using up all system resources, eventually leading to the system slowing down or crashing if there is no process limit set.

FelixJN
  • 13,566
John
  • 17,011
1

If you don't redefine :, it has a default meaning as a no-op. You can see its documentation with help :

% help :
:: :
    No effect; the command does nothing.  A zero exit code is returned.

Legend has it that long ago, in the dawn of the internet, this was a way to put text in your shell scripts that would not be executed. A sort of comment, in other words. I don't have a written source for this.

alexis
  • 5,759
  • Actually, *before* the dawn of the Internet, the shell (the Thompson shell) didn’t have ifthenfi; it had only if … command, with goto (as mentioned in John Mashey’s Shell history article, which you previously linked to). Presumably since : was associated with labels (e.g., in C), it was introduced into the shell as a no-op to allow its use as a label; e.g., goto foo would search for and jump to a : foo line. Then people realized that it was a handy way to put comments into a script. – G-Man Says 'Reinstate Monica' Jan 31 '17 at 22:25
  • You guess right: Comment lines that begin with a colon take advantage of the label syntax. The colon actually defined an empty label (since there's nothing before the colon), and the rest of the line is effectively a comment. I left out these details since I didn't (and still don't) have a source. Bash no longer supports the full syntax, but in ksh you can still write foo: ... instead of : ... (only the second form is documented; both forms perform variable assignments given in the rest of the line). – alexis Feb 05 '17 at 09:06
  • No, I didn’t guess; I stated facts that I know. (Except for the speculation about why Thompson chose the colon to be the symbol that introduces a label; it’s hard to know what a developer was thinking when he wrote a program.) – G-Man Says 'Reinstate Monica' Feb 05 '17 at 23:54
  • Sorry, no disrespect intended. I guess you meant that "presumably" to cover less than it seems to. – alexis Feb 07 '17 at 08:00