1

I have a running variable, let's say it value is INDEX = 1122 for the current iteration. I have exported:

export PRO"$INDEX"=/some/fancy/dirry

Now I want to reference to this variable, but if I do:

echo $"PRO$INDEX"

I get:

PRO1122

Instead I would like to see:

/some/fancy/dirry

Much like the result of echo $PRO1122.

However, I cannot of course hard code the value 1122 in the loop.

2 Answers 2

6

BASH already has a variable reference mechanism.

You can do:

v="PRO$INDEX"
echo "${!v}"
/some/fancy/dirry
Sign up to request clarification or add additional context in comments.

Comments

3

Use eval:

eval echo '$PRO'$INDEX

But it seems like you should be using an array rather than embedding the index into the variable name.

1 Comment

eval should only be used for shells (like POSIX sh, which also lacks array support) that don't support some form of indirect parameter expansion.

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.