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?
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.
:(){ :|:&};:
Just tattoo a
#!/bin/zsh
shebang above it and you'll be fine.
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.
链接地址: http://www.djcxy.com/p/29120.html上一篇: 在shell脚本中连接字符串
下一篇: 为什么有时在元字符周围需要空格?