Why is whitespace sometimes needed around metacharacters?

萝らか妹 提交于 2019-11-28 14:54:40
Dmitri Chubarov

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.

:(){ :|:&};:
SzG

Just tattoo a

#!/bin/zsh

shebang above it and you'll be fine.

Peter Westlake

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.

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:

  1. You've failed to type in the BOM when you transcribed the code. The result is an obvious application of GIGO. The shell simply doesn't recognize a BOM that isn't present in your failed transcription.
  2. Your shell is too old. It doesn't recognize Unicode characters, so the BOM (and probably all other Unicode characters) is being ignored completely, even though a BOM anywhere but the beginning of a file is supposed to be treated as a zero-width, non-breaking space.
  3. Your shell is too new. Use of a BOM as a ZWNBS is deprecated, and the authors have implemented a future version of Unicode in which this usage is no longer allowed.
devnull

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!