Search code examples
zshvariable-expansionparameter-expansion

Nested parameter expansion not working as expected


zsh says:

${name}

The value, if any, of the parameter name is substituted.

It then also says:

If a ${...} type parameter expression or a $(...) type command substitution is used in place of name above, it is expanded first and the result is used as if it were the value of name.

My question is why does the following not work then, assuming I am understanding it correctly, i.e. variable names can be nested?

echo $ab $fooz           
foo 123
echo "${${ab}z}"         
zsh: bad substitution
Expected:
123

Solution

  • What did you expect echo "${${ab}z}" to do? I believe you wanted it to evaluate ${ab} then append a literal z and then interpret the whole thing as if it were a variable name, thus ending up with 123.

    Here is your desired outcome with some steps that show us getting there:

    $ ab=foo fooz=123
    $ echo $ab $fooz
    foo 123
    $ echo ${(P)ab}
    
    $ echo ${(P)ab}z
    z
    $ echo ${${(P)ab}z}
    123
    

    (Bash can do ${!ab} but can't nest to do the final step. POSIX can't do anything of the sort.)

    This uses Zsh's parameter expansion P flag.