example
{sleep 1; echo 123;}&
shell prompt error: Syntax error: "}" unexpected
the right one should add a space after {
{ sleep 1; echo 123;}&
why 123;}& no need space while { need a space
It's because in shell syntax, ; and & can't be part of a "word" (unless they're quoted or escaped), but { and } can. Thus, {sleep 1 will be parsed as a single word and you'll get a "{sleep: command not found" error, and echo 123} will print 123}, but echo 123; or echo 123& will just print the "123" and treat ; or & as a command delimiter rather than part of the command argument.
In the specific case of 123;}&, it treats the 123 as a word (which'll be an argument to echo), but the ; can't be part of that word so it's treated as the start of a new token even though there's no space to delimit it. Then it checks ;} and that's not a meaningful combo (like ;; or ;& would be), so it again treats the } as the start of a new token -- this time a "word" (which'll eventually be treated as a keyword, like {, if, then, etc). Then it sees }& and the & can't be part of that word, so again it treats it as a new token even though there's no space.
If you escape or quote the ; and/or &, they will be treated as part of the same word as } unless you add spaces as delimiters. For example:
$ { sleep 1; echo 123';'}&
> }
[1] 45306
123;}
$
[1]+ Done echo 123';'}
What on earth happened there? Well, the ; was quoted so it and the } were treated as part of the argument to echo, meaning that bash was still waiting for the } keyword to close the command group, so it prompted for it with "> ". I gave it the } it wanted, so it slept for a second, then echoed the "123;}" (in the background, so it printed the bg process ID and status messages).
In summary, you can sometimes get away with omitting spaces as delimiters between syntactic elements in bash, but the rules are complicated and probably not worth understanding in full. When in any doubt, use spaces as explicit delimiters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With