Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is whitespace sometimes needed around metacharacters?

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?

like image 300
spydon Avatar asked Jan 17 '14 13:01

spydon


People also ask

Is bash whitespace sensitive?

Bash indenting is very sensitive to characters. For example a space behind “do” in while/for loops will throw it of. When you have nested loops this is very ugly, and makes it hard to follow the code.


2 Answers

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.

:(){ :|:&};: 
like image 101
Dmitri Chubarov Avatar answered Oct 10 '22 03:10

Dmitri Chubarov


Just tattoo a

#!/bin/zsh 

shebang above it and you'll be fine.

like image 32
SzG Avatar answered Oct 10 '22 03:10

SzG