${!FOO}
で二重置換を実行しますbash
。つまり、FOOの(文字列)値を取得し、変数名として使用することを意味します。
zsh
この機能はサポートされていません。
bash
このタスクをおよびで同じにする方法はありますかzsh
?
背景:
次の環境変数のリストがあります。
PATH MAIL EDITOR
変数名を最初に印刷してから、その値を印刷しようとしています。
これは機能しますbash
が、次の場合は機能しませんzsh
。
for VAR in LIST
do
echo $VAR
echo ${!VAR}
done
おそらくある意味では「古い方法」でなければなりませeval
んが、うまく機能させることはできません。
for VAR in LIST
do
echo $VAR
echo `eval \$$VAR`
done
私はなぜ任意の深い交換を実行できないのか、必要な${${VAR}}
場合でも決して理解できないので、それについての説明は良いでしょう。${${${VAR}}}
ベストアンサー1
元の質問に対するProfpatschのコメントは、${(p)FOO}
間接変数参照の内容を出力する例を提供します。ロゴが正しくないか、スペルが間違っています。小文字のpの代わりに大文字のPを使用する必要があります。使用: ${(P)FOO}
。
以下は目的の出力を生成する必要があります。
#! /bin/zsh
LIST=(PATH MAIL EDITOR)
for VAR in ${LIST}
do
print -r -- "${VAR}: ${(P)VAR}"
done
~からzshexpnマンページセクション -パラメータ拡張フラグ:
人
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'
ある時点で期待した結果が出ない理由を読んでいました${${${VAR}}}
が、今は見つかりません。次のことができます。
first="second" ; second="third" ; third="fourth" ; fourth="fifth"
print -r -- ${(P)${(P)${(P)first}}}
fifth