Skip to content

zsh: command grouping {list} without leading space corrupts output or fails to parse #1295

@LangLangBart

Description

@LangLangBart

While standard POSIX shells are very strict about spaces and semicolons, Zsh is
more flexible:

⚠️ Unexpected formatting

./shfmt --language-dialect zsh <<<'{echo FOO; seq 3}'
# {echo FOO
# seq 3}

Parse failure missing space on left and right side and with ;

./shfmt --language-dialect zsh <<<'{echo FOO; seq 3;}'
# <standard input>:1:18: `}` can only be used to close a block

Parse failure missing space on left or right side and without ;

./shfmt --language-dialect zsh <<<'{echo FOO; seq 3 }'
# <standard input>:1:18: `}` can only be used to close a block
./shfmt --language-dialect zsh <<<'{ echo FOO; seq 3}'
# <standard input>:1:1: reached EOF without matching `{` with `}`

✅ The canonical spaced form formats correctly

./shfmt --language-dialect zsh <<<'{ echo FOO; seq 3; }'
./shfmt --language-dialect zsh <<<'{ echo FOO; seq 3 }'

zsh docs

This terminator may optionally be omitted from the last sublist in the list
when the list appears as a complex command inside ‘(...)’ or ‘{...}’.

https://zsh.sourceforge.io/Doc/Release/Shell-Grammar.html#Simple-Commands-_0026-Pipelines

NOTE: I did not find explicit mention in the zsh documentation that the space
can be omitted; I only verified this by testing it directly, and it appears that
zsh allows it.

additional notes

The equivalent () subshell formats all spacing variants correctly:

./shfmt --language-dialect zsh <<<'(echo FOO; seq 3)'    #
./shfmt --language-dialect zsh <<<'(echo FOO; seq 3;)'   #
./shfmt --language-dialect zsh <<<'(echo FOO; seq 3 )'   #
./shfmt --language-dialect zsh <<<'( echo FOO; seq 3)'   #
./shfmt --language-dialect zsh <<<'( echo FOO; seq 3 )'  #

# (
#         echo FOO
#         seq 3
# )

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions