Reputation: 11512
A few months ago I tattooed a fork bomb on my arm, and I skipped the whitespaces, because I think it looks nicer without them. But to my dismay, sometimes (not always) when I run it in a shell it doesn't start a fork bomb, but it just gives a syntax error.
bash: syntax error near unexpected token `{:'
Yesterday it happened when I tried to run it in a friend's Bash shell, and then I added the whitespace and it suddenly worked, :(){ :|:& };:
instead of :(){:|:&};:
Does the whitespace matter; have I tattooed a syntax error on my arm?!
It seems to always work in zsh, but not in Bash.
A related question does not explain anything about the whitespaces, which really is my question; Why is the whitespace needed for Bash to be able to parse it correctly?
Upvotes: 549
Views: 81490
Reputation: 123458
and then I added the whitespace and it suddenly worked ...
It's because of how the shell parses. You need a space after the function definition begins, i.e. after the {
.
foo() { echo hey& }
foo() { echo hey&}
foo(){ echo hey&}
are valid. On the other hand,
foo() {echo hey&}
isn't.
You actually need a tatoo like this:
From the source:
/* We ignore an open brace surrounded by whitespace, and also
an open brace followed immediately by a close brace preceded
by whitespace. */
Omitting a space after the {
causes the {echo
to be interpreted as a single token.
An equivalent form of
:(){ :|:& };:
would be
:(){
:|:& };:
Note that there is no space after {
in the alternate version, but a line-break causes the shell to recognize {
as a token.
Upvotes: 39
Reputation: 17159
There is a list of characters that separate tokens in BASH. These characters are called metacharacters and they are |
, &
, ;
, (
, )
, <
, >
, space and tab. On the other hand, curly braces ({
and }
) are just ordinary characters that make up words.
Omitting the second space before }
will do, since &
is a metacharacter. Therefore, your tattoo should have at least one space character.
:(){ :|:&};:
Upvotes: 271
Reputation: 490098
Although not easily visible in the tatoo font, there's actually a Byte-Order Mark (BOM) between the brace and the colon (you may have been sufficiently intoxicated when you got the tatoo that you didn't notice it, but it's really there). This leaves three obvious possibilities:
Upvotes: 40
Reputation: 5036
Braces are more like odd keywords than special symbols, and do need spaces. This is different to parentheses, for example. Compare:
(ls)
which works, and:
{ls}
which looks for a command named {ls}
. To work, it has to be:
{ ls; }
The semicolon stops the closing brace being taken as a parameter to ls
.
All you have to do is tell people that you are using a proportional-font with a rather narrow space character.
Upvotes: 50