Q: Variable name within a variable

Q: Variable name within a variable

Post by Aengus Stewar » Tue, 08 Sep 1998 04:00:00

Feeling very stupid this morning and cant sort this problem out, help
would be appreciated.

Say I have the following sort of script (I know it is not right), its
for illustration purposes.


for EXT in 1 2
echo $DIR

Of course, what I would like to see output is


In the above example, it shouts bad substitution.  I have tried a number
of quoting variations without luck.
Am I barking completely up the wrong tree, is there another way to do
I need the for loop though as there are other things apart from this
variable I have to manipulate.



Computational Genome Analysis Laboratory        Tel: +44 (0)171 269 3679
Imperial Cancer Research Fund
Lincoln's Inn Fields, Holborn, London, WC2A 3PX, UK


Q: Variable name within a variable

Post by Clyde Ingra » Tue, 08 Sep 1998 04:00:00

Aengus, let me reveal the Secret of All Eval ...

Inside the loop, try:

eval echo \$DIR$EXT

The eval command forces the shell to 'evaluate' meta-characters first,
before processing the line as normal.  So on the first evaluation, the
backslash is stripped, the first dollar, being protected by backslash, is
left as a dollar, but the $EXT is 'evaluated' to 1 or 2.  This gives the
shell the command:

echo $DIR1
echo $DIR2

Next, the shell processing this as normal, evaluating either $DIR1 or $DIR2,
and you see either


Hope this helps.
Regards, Clyde


Q: Variable name within a variable

Post by brian hile » Wed, 02 Dec 1998 04:00:00

> Feeling very stupid this morning ...

The "typeset -n <varname>" builtin exists (only in ksh93, alas) to
transparently do exactly this.



1. Variable substitution within a variable name

Anyone know of a way to use embedded variable names?

i am trying to setup a shell menu for a user to be able to choose a
printer based on their location.  Since their are different numbers of
printers at each location, i am trying to make it dynamic, not static.
I have tried many combinations to no avail so far.

The test script below gets a dynamic list of printers for a particular
store, then should present a menu of those printers for the user to
select. in this case, there are 4 printers so the menu displayed
should be, for example:

1 for printer store032a
2 for printer store032b
3 for printer store032c
4 for printer store032d

Not quite what I am getting.

lpstat -a | grep store032 | awk '{ print $1 }' |
while read prn
   out{$i}=`echo ${prn}`
   echo "$i for printer ${out${i}}"
   i=`expr $i + 1`

Result of running the above script:
printer_names.sh: out1=store032a: not found
printer_names.sh: bad substitution

Anyone have a way to do this, preferably in the bourne shell?

Robert A. Johnson
Systems Administrator
Cost-U-Less Inc.
8160 304th Ave. SE
Bld. 3 Suite A
Preston, WA 98050
425-222-5022 #161
425-417-2591 (cell)

2. Archive Tape Drive Installation

3. Expanding variables within variables

4. write in a module ?

5. bourne shell variable within a variable

6. Router: Duplicated MAC Address in 2.5.1

7. a variable within a variable

8. How set up X11R6/local heirarchy?

9. Cshel: Variables within variables

10. Evaluating a variable within a variable

11. Setting variables in a variable array of null variables

12. Evaluating variable names stored in variables

13. Variable variable names is csh