Bash 间接引用:${!t} 符号和 eval
Bash indirect references: ${!t} notation and eval
我刚刚想到 bash 的间接引用 ${!t}
的缩写。我习惯于在 $$t
这种间接引用之前加上 eval
。但是,使用 ${!t}
符号似乎我不需要这样做:
# d=e e=f; eval y=${!d}; echo $y
f
# d=e e=f; y=${!d}; echo $y
f
为什么这里不需要eval?是不是隐式调用了?
来自 Bash 参考手册的 3.5.3 Shell Parameter Expansion 部分:
If the first character of parameter is an exclamation point (!), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
eval $$t
间接版本手动导致对行内容进行两次评估。第一个获取间接变量名,第二个获取其值。
我刚刚想到 bash 的间接引用 ${!t}
的缩写。我习惯于在 $$t
这种间接引用之前加上 eval
。但是,使用 ${!t}
符号似乎我不需要这样做:
# d=e e=f; eval y=${!d}; echo $y
f
# d=e e=f; y=${!d}; echo $y
f
为什么这里不需要eval?是不是隐式调用了?
来自 Bash 参考手册的 3.5.3 Shell Parameter Expansion 部分:
If the first character of parameter is an exclamation point (!), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
eval $$t
间接版本手动导致对行内容进行两次评估。第一个获取间接变量名,第二个获取其值。