bash indirection and/or nameref
Comming late on this question, and because no other answer tell about nameref...
Using ${!var}
indirection syntax:
~$ someVariable='Some content'
~$ var=someVariable
~$ echo $var
someVariable
~$ echo ${!var}
Some content
Using namref (declare -n
) syntax
By using nameref you could not only show content of variable, but you can populate variable and get or set attributes.
~$ someVariable='Some content'
~$ declare -n var=someVariable
~$ echo $var
Some content
~$ echo ${var@A}
someVariable='Some content'
This syntax is usefull for functions:
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"
}
(Nota: This function is only a sample. This won't expand correctly arrays and associative arrays!)
Then
~$ 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
~$ declare -ir answerOfUltimateQestionOfLiveUniverseAndEverything=42
~$ showVarDetail answerOfUltimateQestionOfLiveUniverseAndEverything
Variable '$answerOfUltimateQestionOfLiveUniverseAndEverything' is 2 len, has [ir
] flags and contain: 42
Populating variable values using nameref
This could work in both ways!
Here is a little sample function to run with two variable names as arguments. First variable should contain a string and second variable will be populated by 1st character of 1st variable content, then 1st variable content will be shifted by 1 character:
shift1char <variable string source> <variable target>
shift1char () {
local -n srcStr=$1 tgtVar=$2;
tgtVar=${srcStr::1} srcStr=${srcStr:1}
}
Then
~$ 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
With some little modifications:
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
should produce:
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\!