1

What am I doing wrong here?

A_B_NAME="something"
X=A
Y=B

RESULT=`echo \${X}_\${Y}_NAME`
echo ${RESULT}

and I'm always getting A_B_NAME as a result, but want "something"

Thanks!
Michael

terdon
  • 234,489
  • 66
  • 447
  • 667
Michael
  • 11
  • 1

1 Answers1

3

You could use eval here (standard):

eval "RESULT=\$${X}_${Y}_NAME"

Or the bash-specific:

varname=${X}_${Y}_NAME
RESULT=${!varname}

And then:

printf '%s\n' "$RESULT"

remember echo can't be used to output arbitrary data, and parameter expansions must be quoted when in list contexts.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501