# How to parse nested variable

**URL:** <https://community.unix.com/t/how-to-parse-nested-variable/285427>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [March 29, 2011, 5:41am UTC](https://community.unix.com/t/how-to-parse-nested-variable/285427 "2011-03-29T05:41:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![apskaushik](https://community.unix.com/letter_avatar/apskaushik/32/5_5575768a8748004e209b776fc1b2916d.png) [@apskaushik](https://community.unix.com/u/apskaushik)\
**Post date:** [March 29, 2011, 5:41am UTC](https://community.unix.com/t/how-to-parse-nested-variable/285427/1 "2011-03-29T05:41:59Z")

</div>

Hi,

I want to parse nested variable in my script.  
for exp-  
c=1  
G1='0318'  
G2='0023'  
G3='3092'  
G4='0014'

while [$c -le 4];do  
g=G$c  
a=$g  
echo "Group=$g and value=$a"  
c=`expr $c + 1`  
done

final output are as -  
---------------------------  
Group=G1 and value=G1  
Group=G2 and value=G2  
Group=G3 and value=G3  
Group=G4 and value=G4

while i need output as -  
----------------------------  
Group=G1 and value=0318  
Group=G2 and value=0023  
Group=G3 and value=3092  
Group=G4 and value=0014

any help is appreciated.

Thanks  
Amit

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [March 29, 2011, 5:54am UTC](https://community.unix.com/t/how-to-parse-nested-variable/285427/2 "2011-03-29T05:54:54Z")

</div>

try to change

```nohighlight
echo "Group=$g and value=$a" 

```

with

```nohighlight
echo "Group=$g and value=$(eval echo \$$a)" 

```

Or

your while loop can be replaced with smtng like

```nohighlight
for i in G1 G2 G3 G4
do
echo "Group=$i and value=$(eval echo \$$i)"
done

```

---

<div class="post-metadata">

**Author:** ![acmvillareal](https://community.unix.com/letter_avatar/acmvillareal/32/5_5575768a8748004e209b776fc1b2916d.png) [@acmvillareal](https://community.unix.com/u/acmvillareal)\
**Post date:** [March 29, 2011, 6:06am UTC](https://community.unix.com/t/how-to-parse-nested-variable/285427/3 "2011-03-29T06:06:01Z")

</div>

1.txt  
G1='0318'  
G2='0023'  
G3='3092'  
G4='0014'

```nohighlight
#!/bin/sh

C=1

while read LINE
do
if [$C -le 4]
then
   G="G$C"
   A=`echo $LINE | awk '{print substr($0,5,4)}'`
   echo "Group=$G and value=$A"
   C=`expr $C + 1`
fi
done < 1.txt

```

---

<div class="post-metadata">

**Author:** ![apskaushik](https://community.unix.com/letter_avatar/apskaushik/32/5_5575768a8748004e209b776fc1b2916d.png) [@apskaushik](https://community.unix.com/u/apskaushik)\
**Post date:** [March 30, 2011, 1:20am UTC](https://community.unix.com/t/how-to-parse-nested-variable/285427/4 "2011-03-30T01:20:21Z")

</div>

Hi,

Thanks for your valuable help, it solved my problem.

thanks  
Amit

---

<div class="post-metadata">

**Author:** ![acmvillareal](https://community.unix.com/letter_avatar/acmvillareal/32/5_5575768a8748004e209b776fc1b2916d.png) [@acmvillareal](https://community.unix.com/u/acmvillareal)\
**Post date:** [March 30, 2011, 1:33am UTC](https://community.unix.com/t/how-to-parse-nested-variable/285427/5 "2011-03-30T01:33:11Z")

</div>

Anytime.:b:
