gpt4 book ai didi

bash - 什么是间接扩张? ${!var*} 是什么意思?

转载 作者:行者123 更新时间:2023-11-29 08:39:47 25 4
gpt4 key购买 nike

我正在阅读“Bash 初学者指南”。它说:

If the first character of PARAMETER is an exclamation point, 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.

给出的例子是:

franky ~> echo ${!N*}
NNTPPORT NNTPSERVER NPX_PLUGIN_PATH

这里不太明白:

the value of the variable formed from the rest of PARAMETER

因为PARAMETER就是!N*,那么

the rest of PARAMETER

只是 N*。这怎么会形成变数呢? Bash 是否在那里搜索了所有可能的命令?

最佳答案

如果您阅读了 bash 手册页,它基本上证实了您所说的内容:

If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. 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.

${!prefix*} Names matching prefix. Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable.

换句话说,您的特定示例 ${!N*} 是您引用的规则的异常(exception)。但是,它确实在预期情况下如宣传的那样工作,例如:

$ export xyzzy=plugh ; export plugh=cave

$ echo ${xyzzy} # normal, xyzzy to plugh
plugh

$ echo ${!xyzzy} # indirection, xyzzy to plugh to cave
cave

关于bash - 什么是间接扩张? ${!var*} 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8515411/

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