简体   繁体   中英

Variable substitution in Shell script

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?

You can use indirect var reference:

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

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

Output:

true

I'm doing it like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM