Skip to content

zsh error on regex [[:space]] #1288

Closed
Closed

Description

@charpov

I'm delighted to see support for zsh. Thank you!

I'm rewriting a bash script into zsh and shfmt complains about that line:

grep --max-count 1 "^$1[[:space:]]" $mainroster

with the error:

`[` must follow a name

I'm not sure what the error means.

Interestingly, I use a similar line in my bash version, where it causes no problem:

grep --max-count 1 "^$1[[:space:]]" "$mainroster"

Activity

  1. mvdan commented on Mar 14, 2026

    @mvdan
    Owner

    Thanks, this appears to be a bug, also with a confusing error message.

    It's worth noting that what you have here is rather fragile, i.e. zsh only allows it with numeric parameters, but any other parameter breaks.

    $ echo "$foo[[:space:]]"
    zsh: bad output format specification
    $ echo "$1[[:space:]]" 
    [[:space:]]
    
  2. mvdan commented on Mar 14, 2026

    @mvdan
    Owner

    What I mean by that is that something like "^${1}[[:space:]]" is much less error-prone. But I'll try to fix the bug regardless.

  3. mvdan commented on Mar 14, 2026

    @mvdan
    Owner

    Ah, found the part of man zshall which explains this:

    One final note, not directly related to subscripting: the numeric names of positional parameters (described below) are parsed specially, so for example $2foo is equivalent to ${2}foo. Therefore, to use subscript syntax to extract a substring from a positional parameter, the expansion must be surrounded by braces; for example, ${2[3,5]} evaluates to the third through fifth characters of the second positional parameter, but $2[3,5] is the entire second parameter concatenated with the filename generation pattern [3,5].

  4. added a commit that references this issue on Mar 14, 2026
    7cc9bf5
  5. charpov commented on Mar 21, 2026

    @charpov
    Author

    I agree that ${1}x is safer than $1x, but when I look for robustness and readability, I don't program in shell! 😏
    Thanks for the fix and for shfmt in general. I had been waiting eagerly for zsh support. Now, all I need is zsh support in shellcheck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions