Home > front end >  Shell script to print value of variable recursively
Shell script to print value of variable recursively

Time:11-12

#!/bin/bash
A="X"
X="Y"
B=${$A}
echo $B # expecting output to be 'Y'

Actual output seen : line 4: ${$A}: bad substitution

CodePudding user response:

It's called parameter indirection:

You can use ${!nameref} to treat the value of nameref as a parameter:

A="X"
X="Y"
B=${!A}
echo "$B"

See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html for more info on parameter expansion

  • Related