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

1
  • \${X}_\${Y}_NAME is not the variable A_B_NAME, which is $A_B_NAME. Commented Feb 19, 2020 at 20:13

1 Answer 1

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.

1
  • Thank you Stephane! It works! have a nice evening/day Commented Feb 19, 2020 at 20:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.