什么是间接扩展? $ {!var *}是什么意思? [英] What is indirect expansion? What does ${!var*} mean?

查看:160
本文介绍了什么是间接扩展? $ {!var *}是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读"初学者打击指南".它说:

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

如果PARAMETER的第一个字符是一个感叹号,则Bash将从PARAMETER的其余部分形成的变量的值用作变量的名称;否则,Bash将使用PARAMETER的其余部分形成的变量的值作为变量的名称.然后扩展此变量,并在其余替换中使用该值,而不是PARAMETER本身的值.这就是所谓的间接扩展.

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

我在这里不太明白:

PARAMETER的其余部分组成的变量的值

the value of the variable formed from the rest of PARAMETER

因为PARAMETER只是!N*,所以

PARAMETER

只是N*.这如何形成变量? Bash在那里搜索了所有可能的命令吗?

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

推荐答案

如果您阅读了bash手册页,则基本上可以确认您所说的内容:

If you read the bash man 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.

但是,请从那里继续阅读

However, reading on from there:

以下是${!prefix*}${!name[@]}的扩展.

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

${!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*}是您引用的规则的例外.它确实可以正常工作,例如:

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

$ export xyzzy=plugh ; export plugh=cave

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

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

这篇关于什么是间接扩展? $ {!var *}是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆