3.22: How do I get a variable's value to be evaluated as another variable?
The problem is that you have a variable $E containing the string
`EDITOR', and a variable $EDITOR containing the string `emacs',
or something such. How do you get from $E to emacs in one easy
stage?
There is no standard single-stage way of doing this. However, there
is a zsh idiom (available in all versions of zsh since 3.0) for this:
print ${(e)E:+\$$E}
Ignore the `(e)' for now. The `:+' means: if the variable
$E is set, substitute the following, i.e. `\$$E'. This is
expanded to `$EDITOR' by the normal rules. Finally, the `(e)' means
`evaluate the expression you just made'. This gives `emacs'.
For a standard shell way of doing this, you are stuck with `eval':
eval echo \$$E
produces the same result.
Versions since 3.1.6 allow you to do this directly with a new flag;
`${(P)E}'.
As a slight aside, sometimes people note that the syntax `${${E}}'
is valid and expect it to have this effect. It probably ought to, but
in the early days of zsh it was found convenient to have this way of
producing different substitutions on the same parameter; for example,
`${${file##**/}%.*}' removes everything up to the last slash in
`$file', then everything from the last dot on, inclusive (try
it, this works). So in `${${E}}', the internal `${...}'
actually does nothing.