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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8515411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:21:34  来源:igfitidea点击:

What is indirect expansion? What does ${!var*} mean?

bashindirection

提问by athos

I'm reading "Bash Guide for Beginners". It says:

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

If the first character of PARAMETERis an exclamation point, Bash uses the value of the variable formed from the rest of PARAMETERas 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 PARAMETERitself. This is known as indirect expansion.

如果 of 的第一个字符PARAMETER是感叹号,则 Bash 使用由其余部分组成的变量的值作为变量PARAMETER名;然后扩展这个变量,并且该值用于替换的其余部分,而不是PARAMETER它本身的值。这称为间接扩展。

The example given is:

给出的例子是:

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

I don't quite understand here:

我不太明白这里:

the value of the variable formed from the rest of PARAMETER

由其余部分形成的变量的值 PARAMETER

As the PARAMETERis just !N*, then

由于PARAMETER!N*,那么

the rest of PARAMETER

其余的 PARAMETER

is just N*. How could this form a variable? Did Bash search all possible command there?

只是N*. 这怎么会形成一个变量?Bash 在那里搜索了所有可能的命令吗?

回答by paxdiablo

If you read the bashman page, it basically confirms what you have stated:

如果您阅读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.

如果参数的第一个字符是感叹号 ( !),则引入了一个变量间接级别。Bash 使用由参数的其余部分形成的变量的值作为变量的名称;这个变量然后被扩展,并且该值用于替换的其余部分,而不是参数本身的值。这称为间接扩展。

However, reading on from there:

但是,从那里继续阅读:

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 IFSspecial variable.

对此的例外是下面描述的${!prefix*}和的扩展${!name[@]}

${!prefix*}名称匹配前缀。扩展到名称以前缀开头的变量名称,由IFS特殊变量的第一个字符分隔。

In other words, your particular example ${!N*}is an exceptionto the rule you quoted. It does, however, work as advertised in the expected cases, such as:

换句话说,您的特定示例${!N*}是您引用的规则的例外。但是,它确实在预期的情况下像广告一样工作,例如:

$ export xyzzy=plugh ; export plugh=cave

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

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

回答by Kevin

There appears to be an exception when the given "indirection" ends in a *, as it does here. In this case, it gives all variable names that start with the part you specified (Nhere). Bash can do that because it tracks variables and knows which ones exist.

当给定的“间接”以 a 结尾时,似乎有一个例外*,就像这里一样。在这种情况下,它给出了以您指定的部分(N此处)开头的所有变量名称。Bash 可以做到这一点,因为它跟踪变量并知道存在哪些变量。

True indirection is this:
Say I have a variable $VARIABLEset to 42, and I have another variable $NAMEset to VARIABLE. ${!NAME}will give me 42. You use the value of one variable to tell you the name of another:

真正的间接是这样的:
假设我有一个变量$VARIABLE设置为42,而另一个变量$NAME设置为VARIABLE${!NAME}会给我42。您可以使用一个变量的值来告诉您另一个变量的名称:

$ NAME="VARIABLE"
$ VARIABLE=42
$ echo ${!NAME}
42

回答by tpg2114

Yes, it searches for all possible expansions of variables after the !. If you had done:

是的,它会在 ! 之后搜索所有可能的变量扩展。如果你做过:

echo ${!NP*}

you would get only NPX_PLUGIN_PATH.

你只会得到NPX_PLUGIN_PATH.

Consider the following example:

考虑以下示例:

:~> export myVar="hi"
:~> echo ${!my*}
    myVar
:~> export ${!my*}="bye"
:~> echo $myVar
    bye

回答by Ignacio Vazquez-Abrams

You've hit an exception in indirection processing, where if the last character is *, all variables that have the prefix given before will be returned.

您在间接处理中遇到了异常,如果最后一个字符是*,则将返回所有具有之前给出的前缀的变量。

回答by Margach Chris

You can refer to this GNU doc for bash for authoritative information

你可以参考这个 GNU doc for bash 获取权威信息

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion

But basically, indirect expansion is not performed on ${!prefix*}as one of the exceptions, in your example, N is the prefix.

但基本上,间接扩展不会${!prefix*}作为例外之一执行 ,在您的示例中, N 是前缀。

The Document will explain what indirect expansion is in bash

该文档将解释什么是 bash 中的间接扩展