# Shell script variable names created dynamically

**URL:** <https://community.unix.com/t/shell-script-variable-names-created-dynamically/296570>\
**Category:** Shell Programming and Scripting\
**Created:** [October 4, 2011, 9:42am UTC](https://community.unix.com/t/shell-script-variable-names-created-dynamically/296570 "2011-10-04T09:42:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![u671296](https://community.unix.com/letter_avatar/u671296/32/5_5575768a8748004e209b776fc1b2916d.png) [@u671296](https://community.unix.com/u/u671296)\
**Post date:** [October 4, 2011, 9:42am UTC](https://community.unix.com/t/shell-script-variable-names-created-dynamically/296570/1 "2011-10-04T09:42:40Z")

</div>

Hi,

I'm trying to use a config file to define frequencies for checking log files. If the config file contains a frequency it will be used else a default value. The format of the config file (and hence the environment variable) is  
FREQ\_log\_logname=value

A test shell script as below:

```nohighlight
FREQ_log_aaa="DAILY"
FREQ_log_bbb="WEEKLY"
FREQ_log_ccc="DAILY"
for i in aaa bbb ccc
do
logfreq="$FREQ_log_$i"
echo "$logfreq";
done

```

outputs:

```nohighlight
aaa
bbb
ccc

```

rather than the desired:

```nohighlight
DAILY
WEEKLY
DAILY

```

I've spent the last hour trying eval but get the same or bad substitution errors. Does anybody know how to do this ? Thanks.

---

<div class="post-metadata">

**Author:** ![ahamed101](https://community.unix.com/user_avatar/community.unix.com/ahamed101/32/1118_2.png) [@ahamed101](https://community.unix.com/u/ahamed101)\
**Post date:** [October 4, 2011, 9:46am UTC](https://community.unix.com/t/shell-script-variable-names-created-dynamically/296570/2 "2011-10-04T09:46:10Z")

</div>

Try this change in your code...

```nohighlight
...
eval logfreq="\$FREQ_log_$i"
...

```

PS : Use code tags!

--ahamed

---

<div class="post-metadata">

**Author:** ![u671296](https://community.unix.com/letter_avatar/u671296/32/5_5575768a8748004e209b776fc1b2916d.png) [@u671296](https://community.unix.com/u/u671296)\
**Post date:** [October 4, 2011, 9:53am UTC](https://community.unix.com/t/shell-script-variable-names-created-dynamically/296570/3 "2011-10-04T09:53:28Z")

</div>

> [@ahamed101](#):
>
> Try this change in your code...
> 
> ```plaintext
> ...
> eval logfreq="\$FREQ_log_$i"
> ...
> 
> ```
> 
> PS : Use code tags!
> 
> --ahamed

Thanks ahamed ! Hadn't realised the first $ would need escaping. Sorry about the code tags, this is my first post on this site. Thanks again.
