1

How can I do the following in Unix:

1) Variable xxx_yyy=12345
2) Variable aaa=yyy

How can I evaluate xxx_$aaa to give me 12345.

Please advise.

0

2 Answers 2

2

In bash use indirect parameter expansion:

varname=xxx_$aaa
echo ${!varname}

However, dynamic variable names are usually tricky to handle. Easier to use an associative array:

declare -A xxx
xxx[yyy]=12345
aaa=yyy
echo ${xxx[$aaa]}
Sign up to request clarification or add additional context in comments.

Comments

1

In bash (and probably others: specify if you want another), use eval. Don't forget to escape the first $ as shown:

eval echo \$xxx_$aaa

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.