Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!mit-eddie!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!boingo.med.jhu.edu!haven!adm!news From: protin@pica.army.mil (Arthur W. Protin Jr.) Newsgroups: comp.unix.internals Subject: Re: UNIX-WIZARDS Digest V11#044 Message-ID: <25117@adm.brl.mil> Date: 27 Nov 90 17:44:02 GMT Sender: news@adm.brl.mil Lines: 88 In article <1990Nov27.003659.3521@informix.com> housed@infmx.informix.com (Darryl House) writes: -> ->The following is a Bourne shell script that illustrates ->what I am trying to do: set a shell variable that ->contains the contents of another one that is referred ->to by concatenating two others. Sufficiently confusing? ->Yeah, I thought so, too. . . . ->-------------------------------------------------------- ->#! /bin/sh -> -># what I want is to be able to set -># the ITERATION variable to be the expanded -># version of $PREFIX$SUFFIX, i.e. the first iteration -># would be the contents of $firsttime, the second -># would be the contents of $secondtime. The following -># code gives me errors, and everything I try either gets -># the same substitution failure or just echoes the name -># of the variable, i.e. ($firsttime and $secondtime). -> ->echo -> ->firsttime="first_time" ->secondtime="second_time" -> ->PREFIX_WORDS="first second" ->SUFFIX="time" -> ->for PREFIX in $PREFIX_WORDS ->do -> -># the following line doesn't work, but -># sort of illustrates what I want to do. -># I want this to be ITERATION=$firsttime the first time -># through and ITERATION=$secondtime the second time. -> -> ITERATION=${$PREFIX$SUFFIX} -> -> echo 'Iteration is $ITERATION' ->done -> ->echo -> ->exit 0 When you try to read a value as a variable it is a nice idea to consider the ( otherwise neglected ! ) eval. Try this as a substitute : #! /bin/sh firsttime="first_time" secondtime="second_time" PREFIX_WORDS="first second" SUFFIX="time" for PREFIX in $PREFIX_WORDS do # # this does what you wanted # eval "ITERATION=\$$PREFIX$SUFFIX" echo Iteration is $ITERATION # # this show how to assign values to variable whose name you compute. # eval "$PREFIX$SUFFIX=${PREFIX}.time" eval "ITERATION=\$$PREFIX$SUFFIX" echo Iteration is $ITERATION done echo ------------------ While this resembles the solution offered by Saumen K Dutta, it does not require the spawning of a subshell and as such should be faster. It also includes the demonstration of assignment to calculated variable names. Arthur Protin These are my personal views and do not reflect those of my boss or this installation.