"$ {!var}"是什么?在shell脚本中意味着什么? [英] What does "${!var}" mean in shell script?

查看:1191
本文介绍了"$ {!var}"是什么?在shell脚本中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个满足以下条件的代码块,不知道它到底能做什么.

I have a code block with below condition, not sure what exactly it does.

$var = "${args}_Some_Text"
if [ "${!var}"  == '' ];then
     echo "$var is not defined !!!"
fi 

推荐答案

这称为变量间接扩展.

$ hello="this is some text"   # we set $hello
$ var="hello"                 # $var is "hello"
$ echo "${!var}"              # we print the variable linked by $var's content
this is some text

如您所见,这是定义变量"的一种方法.也就是说,使用内容为另一个变量名称的变量.

As you see, it is a way to define "variable variables". That is, to use variables whose content is the name of another variable.

来自 Bash参考手册→3.5.3 Shell参数扩展:

如果参数的第一个字符是感叹号(!),并且 parameter 不是 nameref ,则会引入变量间接寻址级别. Bash使用由其余参数形成的变量的值作为变量的名称;然后扩展此变量,并在其余替换中使用该值,而不是 parameter 本身的值.这称为间接扩展.如果parameter是 nameref ,它将扩展为parameter引用的变量的名称,而不是执行完整的间接扩展.例外是$ {!prefix *}和$ {!name [@]}的扩展,如下所述.感叹号必须紧跟左括号,以引入间接作用.

If the first character of parameter is an exclamation point (!), and parameter is not a nameref, 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. If parameter is a nameref, this expands to the name of the variable referenced by parameter instead of performing the complete 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.

这篇关于"$ {!var}"是什么?在shell脚本中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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