加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – “${!var}”在shell脚本中的含义是什么?

发布时间:2020-12-15 22:15:30 所属栏目:安全 来源:网络整理
导读:参见英文答案 What is indirect expansion? What does ${!var*} mean?????????????????????????????????????4个 我有一个具有以下条件的代码块,不知道它究竟是做什么的. $var = "${args}_Some_Text"if [ "${!var}" == '' ];then echo "$var is not defined !
参见英文答案 > What is indirect expansion? What does ${!var*} mean?????????????????????????????????????4个
我有一个具有以下条件的代码块,不知道它究竟是做什么的.

$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

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

从Bash Reference Manual → 3.5.3 Shell Parameter Expansion开始:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读