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

前端之家收集整理的这篇文章主要介绍了bash – 什么是间接扩展? ${!var *}是什么意思?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在阅读“初学者的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.

给出的例子是:

06000

我不太明白这里:

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 *}是您引用的规则的一个例外。但是,它可以按预期的情况发布,如:

$ export xyzzy=plugh ; export plugh=cave

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

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

猜你在找的Bash相关文章