gpt4 book ai didi

Bash 间接引用 : ${! t} 符号和 eval

转载 作者:行者123 更新时间:2023-11-29 09:30:07 31 4
gpt4 key购买 nike

我刚刚想到 bash 的间接引用 ${!t} 的简短表示法。我习惯于在 eval 类型的 \$$t 间接引用之前。但是,使用 ${!t} 符号似乎我不需要这样做:

# d=e e=f; eval y=${!d}; echo $y
f
# d=e e=f; y=${!d}; echo $y
f

为什么我们在这里不需要评估?是否隐式调用?

最佳答案

来自 3.5.3 Shell Parameter Expansion 部分Bash 引用手册:

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} 符号和 eval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29123187/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com