Home > Software design >  just started learning shell script, can't solve this problem
just started learning shell script, can't solve this problem

Time:09-22

foo=username
bar=foo

a=$(eval echo \$$bar) # same as ${!bar}

echo $(eval echo \$${a^^}) # expected val

How to get the value of foo through the variable bar, Same as the above output

I know it wants a variable name not a string here.

echo $(eval echo \$${${!bar}^^}) # error bad substitution

CodePudding user response:

BASH The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution. Case conversion is part of parameter and variable expansion. To see the value of the case-converted variable name without assigning another variable you can do:

foo=username
echo \$${foo^^}
$USERNAME
echo $(eval echo \$${foo^^})
stark

CodePudding user response:

I believe this is what you want to start with:

foo=username
bar=$foo

... where username is the literal string, you want to put in the variable, named foo, and $foo is the value of the variable foo, which you want to put in the variable, named bar.

If you want to create another variable, let's say a, which contains the value of bar, you just do:

a=$bar
  • Related