bash的
间接和/或
nameref
对于这个问题回答晚了,而且因为没有其他回答提到
nameref...
使用
${!var}
间接语法:
~$ someVariable='Some content'
~$ var=someVariable
~$ echo $var
someVariable
~$ echo ${!var}
Some content
使用 nameref (declare -n
) 语法
通过使用 nameref,你不仅可以显示变量的内容,还可以填充变量并获取或设置属性。
~$ someVariable='Some content'
~$ declare -n var=someVariable
~$ echo $var
Some content
~$ echo ${var@A}
someVariable='Some content'
这个语法对于函数很有用:
function showVarDetail() {
local -n var=$1
printf 'Variable \47\44%s\47 is %d len, has [%s] flags and contain: %q\n' \
"$1" "${#var}" "${var@a}" "$var"
}
(注意:此函数仅为示例。它无法正确展开数组和关联数组!)
然后
~$ someVar='Hello world!'
~$ showVarDetail someVar
Variable '$someVar' is 12 len, has [] flags and contain: Hello\ world\!
~$ declare -r PI=3.14159265358979323844
~$ showVarDetail PI
Variable '$PI' is 22 len, has [r] flags and contain: 3.14159265358979323844
使用nameref填充变量值
这可以以两种方式工作!
这里有一个小示例函数,使用两个变量名作为参数进行运行。第一个变量应包含一个字符串,而第二个变量将由第一个变量内容的第一个字符填充,然后将第一个变量内容向右移动 1 个字符:
shift1char <variable string source> <variable target>
shift1char () {
local -n srcStr=$1 tgtVar=$2;
tgtVar=${srcStr::1} srcStr=${srcStr:1}
}
那么
~$ someVar='Hello world!'
~$ shift1char someVar someChar
~$ showVarDetail someVar
Variable '$someVar' is 11 len, has [] flags and contain: ello\ world\!
~$ showVarDetail someChar
Variable '$someChar' is 1 len, has [] flags and contain: H
通过一些小修改:
showVarDetail() {
local _nam
for _nam in "$@"; do
local -n _var=$_nam
printf \
'Variable \47\44%s\47 is %d len, has [%s] flags and contain: %q\n' \
"${_nam}" "${#_var}" "${_var@a}" "$_var"
done
}
move1char() {
local -n srcStr=$1 tgtVar=$2
[[ -z $srcStr ]] && return 1
tgtVar+=${srcStr::1} srcStr=${srcStr:1}
}
someVar='Hello world!' target=''
while move1char someVar target;do
showVarDetail someVar target
done
应该产生:
Variable '$someVar' is 11 len, has [] flags and contain: ello\ world\!
Variable '$target' is 1 len, has [] flags and contain: H
Variable '$someVar' is 10 len, has [] flags and contain: llo\ world\!
Variable '$target' is 2 len, has [] flags and contain: He
Variable '$someVar' is 9 len, has [] flags and contain: lo\ world\!
Variable '$target' is 3 len, has [] flags and contain: Hel
Variable '$someVar' is 8 len, has [] flags and contain: o\ world\!
Variable '$target' is 4 len, has [] flags and contain: Hell
Variable '$someVar' is 7 len, has [] flags and contain: \ world\!
Variable '$target' is 5 len, has [] flags and contain: Hello
Variable '$someVar' is 6 len, has [] flags and contain: world\!
Variable '$target' is 6 len, has [] flags and contain: Hello\
Variable '$someVar' is 5 len, has [] flags and contain: orld\!
Variable '$target' is 7 len, has [] flags and contain: Hello\ w
Variable '$someVar' is 4 len, has [] flags and contain: rld\!
Variable '$target' is 8 len, has [] flags and contain: Hello\ wo
Variable '$someVar' is 3 len, has [] flags and contain: ld\!
Variable '$target' is 9 len, has [] flags and contain: Hello\ wor
Variable '$someVar' is 2 len, has [] flags and contain: d\!
Variable '$target' is 10 len, has [] flags and contain: Hello\ worl
Variable '$someVar' is 1 len, has [] flags and contain: \!
Variable '$target' is 11 len, has [] flags and contain: Hello\ world
Variable '$someVar' is 0 len, has [] flags and contain: ''
Variable '$target' is 12 len, has [] flags and contain: Hello\ world\!