Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 12 down vote accepted

{}

          { 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

share|improve this answer
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 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.

share|improve this answer
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 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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