1

I have declared one variable IS_abc=false, on basis of certain condition I am changing value to IS_abc=true

IS_abc=false
declare -a my_arr
my_arr = ('abc' 'pqr' 'xyz')
....
.... // some operation
IS_abc=true
for i in "${my_arr[@]}"
do
    //here i want to access value of $IS_abc  as true
    //how to do this
done

I have tried accessing using $IS_'$i' , but it raising error as invalid substitution

Tell me if I am doing anything wrong here?

2 Answers 2

4

You can use indirect var reference:

my_arr=('abc' 'pqr' 'xyz')
IS_abc=true

var="IS_${my_arr[0]}"
echo "${!var}"

Output:

true
Sign up to request clarification or add additional context in comments.

2 Comments

What does the ! do in "${!var}"?
That is called indirect expansion. This expands to the name of the variable referenced by parameter. So it prints value of IS_abc variable.
1

I'm doing it like this:

value=`eval echo \\${IS_${i}}`

There's probably a better way but this should work.

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.