${!FOO} and zsh

The comment, under the original question, by Profpatsch gives the example ${(p)FOO} to output the content of an indirect variable reference. The flag is incorrect or a typo, it should be a capital P and not a lower case p. Use: ${(P)FOO}.

The following should produce the desired output:

#! /bin/zsh
LIST=(PATH MAIL EDITOR)
for VAR in ${LIST}
do
    print "${VAR}:  ${(P)VAR}"
done

From the zshexpn man page; section - Parameter Expansion Flags:

P

  This forces the value of the parameter name to be interpreted as a further
  parameter name,  whose  value  will  be used where appropriate.  Note that
  flags set with one of the typeset family of commands (in particular case
  trans‐formations) are not applied to the value of name used in this fashion.

  If used with a nested parameter or command substitution, the result of that
  will be taken as a parameter  name  in the  same  way.   For  example,  if
  you  have  `foo=bar'  and `bar=baz', the strings ${(P)foo}, ${(P)${foo}}, and
  ${(P)$(echo bar)} will be expanded to `baz'

At one time I read why ${${${VAR}}} does not produce the output you expected, but at this time I can't find it. You can do something like the following:

first="second" ; second="third" ; third="fourth" ; fourth="fifth"
print ${(P)${(P)${(P)first}}}
fifth

Both bash and zsh have a way to perform indirect expansion, but they use different syntax.

It's easy enough to perform indirect expansion using eval; this works in all POSIX and most Bourne shells. Take care to quote properly in case the value contains characters that have a special meaning in the shell.

eval "value=\"\${$VAR}\""
echo "$VAR"
echo "$value"

${${VAR}} doesn't work because it's not a feature that any shell implements. The thing inside the braces must conform to syntax rules which do not include ${VAR}. (In zsh, this is supported syntax, but does something different: nested substitutions perform successive transformations on the same value; ${${VAR}} is equivalent to $VAR since this performs the identity transformation twice on the value.)


You are not using eval correctly. In your example value of $VAR preceded with a "$" (i.e `$VALUE') would be executed as a command. That's not what you want. You want to evaluate the expansion of a variable whose name is taken from another variable.

$ for i in `echo PATH MAIL EDITOR`; 
    do eval moo="\${$i}" 
    echo $moo 
done
/usr/local/sbin:/usr/local/bin:/usr/sbin:/u (...)
/var/mail/root
nano