I'm trying to figure out how to get var names from arguments on a function.
I know to get the var name you can do it in many ways:
#!/usr/bin/env bash
foo="bar"
var_name=${!foo@}
echo $var_name" = "$foo
var_name=${-+foo}
echo $var_name" = "$foo
var_name=$'foo'
echo $var_name" = "$foo
var_name=$"foo"
echo $var_name" = "$foo
All of these examples produce the output foo = bar
Inside a function, the argument vars are "${1}", "${2}", etc. What I want is to take the var name before passing it as argument to a function from that function. Example:
#!/usr/bin/env bash
function sample() {
echo ${!1@} #This produce en error: test.sh: line 4: ${!1@}: bad substitution
echo ${-+1} #This produce "1" as output
echo $'1' #This produce "1" as output
echo $"1" #This produce "1" as output
}
foo="bar"
sample "${foo}"
Expected output is "foo"
. How can achieve this?