Is there a way to evaluate any string variable, when the string/variable name has been defined inside a script? Answer: Yes. See below. Note how it handles various kinds of whitespace.
#!/bin/bash
function infunction () {
printf '%s\n' '--- next ---'
printf '%s\n' 'hard-coded variable name "dog":'
printf 'BEGIN|'
printf '%s' "$dog"
printf '|END'
printf '\n'
printf '%s\n' 'parameter expansion by printf:'
printf 'BEGIN|'
printf '%s' "${!nameofvariable}"
printf '|END'
printf '\n'
}
nameofvariable='dog'
dog='pig'
infunction
dog=' - pig '
infunction
dog=' '
infunction
dog=$'\n'
infunction
dog=$'\t'
infunction