简体   繁体   English

Bash脚本 - 变量引用中的感叹号

[英]Bash Script - Exclamation Point within Variable Reference

I'm looking over a script (which has been used successfully in the past) which contains the following: 我正在查看一个脚本(过去成功使用过),其中包含以下内容:

node=1
while :
do
    userKey=WEB_${node}_USER
    userVal=`echo ${!userKey}`

I have not been able to figure out why an exclamation point would be added to a variable reference like this. 我无法弄清楚为什么会将感叹号添加到这样的变量引用中。 What purpose does "!" 目的是什么“!” serve in this context? 在这种背景下服务?

It's rare for me to do much scripting so if I am missing any details please let me know and I will try to provide more information. 我很少编写脚本,所以如果我遗漏任何细节请告诉我,我会尝试提供更多信息。 I have not been able to find this answer elsewhere. 我无法在其他地方找到这个答案。

Thanks in advance! 提前致谢!

It's called indirect parameter expansion. 它被称为间接参数扩展。 Where $userKey expands to the value of the variable userKey , ${!userKey} expands to the value of the variable whose name is the value of userKey . 其中$userKey扩展为变量userKey的值, ${!userKey}扩展为名称为 userKey的变量的值 Since usrKey has the value WEB_1_USER (given the current value of $node , ${!userKey} expands to the same result as $WEB_1_USER . 由于usrKey的值为WEB_1_USER (给定$node的当前值, ${!userKey}扩展为与$WEB_1_USER相同的结果。

Its use is somewhat rare, since in many cases (including, it appears, here) an array WEB_USER of user names would be clearer than a set of numbered variables. 它的使用有点罕见,因为在许多情况下(包括,在这里出现),用户名的数组WEB_USER比一组编号变量更清晰。

WEB_USER=(alice bob charlie)

node=1
while :
do
  userVal=${WEB_USER[node]}

在bash中,我认为在双引号中保留元字符状态的唯一字符是美元符号($),反向符号(`)和反斜杠(\\)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM