Search code examples
zsh

Numeric context in zsh array indexing


I have an array schemes, defined by

schemes=( a b c d e )

and want to pick a random element from it. My first attempt was

echo $schemes[1 + (RANDOM % $#schemes)]

This resulted in an invalid subscript error every time I run this (i.e. independent of the random number generated). I then changed the code to

((n=1 + (RANDOM % $#schemes)))
echo $schemes[n]

and it works fine. Why? After all, array indexing imposes the same kind of numeric context as ((....)) does, isn't it? So the two approaches should be equivalent.


Solution

  • Based on the information kindly provided by @rowboat:

    It is a parsing problem. Within square brackets, only certain characters are allowed, and a space is not one of them. When the parser encounters the space, it doesn't look further but throws an invalid subscript error.

    This applies only if array access is lazily written as $schemes[...]. If I write the offending line as

    echo ${schemes[1 + (RANDOM % $#schemes)]}
    

    the space is not an error anymore.