${!FOO} performs a double substitution in bash, meaning it takes the (string) value of FOO and uses it as a variable name.
zsh doesn’t support this feature.
Is there a way to make this work the same in bash and zsh?
Background:
I’ve got a list of environment variables, like
PATH MAIL EDITOR
and want to first print the variable names and afterwards their values.
This works in bash but not zsh:
for VAR in LIST
do
echo $VAR
echo ${!VAR}
done
It should be somehow possible “the old way” with eval, but I can’t get it to work:
for VAR in LIST
do
echo $VAR
echo `eval \$$VAR`
done
I’m never going to understand why I can’t simply do arbitrary deep substitutions like ${${VAR}} or even ${${${VAR}}} if need be, so an explanation for that would be nice, too.
bash, it indeed has the same function (and a lot more), only uses a different pattern, namely${(p)FOO}. – Profpatsch Mar 19 '13 at 13:46(e)parameter expansion flag was added in zsh-2.6-beta17 (May 1996), the(P)flag in 3.1.5-pws-7 (1999). bash's${!var}in 2.0 (December 1996). – Stéphane Chazelas Oct 25 '16 at 22:14