You probably know of that fork bomb:

 :(){ :|:&};: #WARNING: harmful code

I wonder why it is necessary, for it to parse, to include a space after the opening curly brace.



          { list; }

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

In addition to the creation of a subshell, there is a subtle difference between these two constructs due to historical reasons. The braces are reserved words, so they must be separated from the list by blanks or other shell metacharacters. The parentheses are operators, and are recognized as separate tokens by the shell even if they are not separated from the list by whitespace.

// source

  • 1
    I've quibbled with your answer in my own post on this page. It's a function not a command grouping. – donothingsuccessfully Jul 5 '12 at 18:46
  • 3
    @donothingsuccessfully A function body is a command grouping. – Gilles 'SO- stop being evil' Jul 5 '12 at 22:44

I think @rush may be giving a correct if misleading answer here. The fork-bomb defines a function called ":". The code in the curly braces is not executed until the function is called by the final ":". So the curly braces as command grouping and the curly braces as function body are syntactically the same but have different semantics.
From the same document as @rush cites:

Note that for historical reasons, in the most common usage the curly braces that surround the body of the function must be separated from the body by blanks or newlines. This is because the braces are reserved words and are only recognized as such when they are separated from the command list by whitespace or another shell metacharacter. Also, when using the braces, the list must be terminated by a semicolon, a ‘&’, or a newline.

  • 3
    No, rush's answer is correct. The curly braces around a function body are the same syntactic element as when they're not delimited a function body. See the shell grammar rules: a function_body is a compound_command (plus an optional redirect_list). The space is needed because { is a reserved word, not a special character. – Gilles 'SO- stop being evil' Jul 5 '12 at 22:43
  • Yes, rush's answer is correct, as I said. Yes, they are syntactically the same, as I said. But brace_groups behave differently when part of a function and when not. I was using command grouping to describe the latter case and this is what was described in rush's link. – donothingsuccessfully Jul 6 '12 at 7:00
  • 2
    I'm not sure the semantic distinction is significant here, as it is the syntax that matters to the parser. The real answer is { is not an operator but a reserved word, and words need to be separated from other words in order to be considered distinct. – jw013 Oct 31 '12 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.