Path: utzoo!attcan!uunet!clyde.concordia.ca!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!uflorida!novavax!hcx1!brad From: brad@SSD.CSD.HARRIS.COM (Brad Appleton) Newsgroups: comp.unix.questions Subject: Re: unix shell programming question Summary: use "eval" Message-ID: <3106@hcx1.SSD.CSD.HARRIS.COM> Date: 27 Feb 90 15:29:59 GMT References: <504@lkbpyr.UUCP> Sender: news@hcx1.SSD.CSD.HARRIS.COM Organization: Harris Computer Systems, Fort Lauderdale, FL Lines: 87 In article <504@lkbpyr.UUCP> jonas@lkbpyr.UUCP (Jonas Heyman) writes: >Hello, > >Could anyone give me some help: > >On the lines with "echo" I want to have the same output. >Each time the "for loop" goes trough I want to list $str1 and $str2. >How can I do that. > >Sincerely Jonas. > >---------------------------------------- >str1="string 1" >str2="string 2" > >... >... >... > >echo $str1 #This is OK "string 1" >echo $str2 #This is OK "string 2" > >for loop in 1 2 >do > aa=$str$loop #Here I want "string 1" and "string 2" > echo $aa > > echo $str$loop #Here I want "string 1" and "string 2" >done >----------------------------------------- > You need to use "eval" to accomplish this. Eval is a shell builtin which may be used to: * Process the result of an expansion or substitution by a step that proceeds it during command processing * Find the value of a parameter/variable whose value is the name of another parameter/variable * Execute a line that was read from standard input Using "eval" in your example would result in the following (NOTE: I removed the leading '$' from '$str" since "str" is not the name of a shell variable): -------------------------------------------------------------------- str1="string 1" str2="string 2" . . . for loop in 1 2 do aa=str$loop echo `eval echo '$'$aa` ## prints "string 1" when loop=1 ## and prints "string 2" when loop=2 echo `eval echo '$'str$loop` ## same effect as above done -------------------------------------------------------------------- I'm not sure of what it is you want to "echo" however. The output from the script segment above would be: string 1 string 1 string 2 string 2 not: string 1 string 2 string 1 string 2 Hope this helps! +=-=-=-=-=-=-=-=-= "... and miles to go before I sleep." -=-=-=-=-=-=-=-=-=-+ | Brad Appleton | Harris Computer Systems Division | | | 2101 West Cypress Creek Road | | brad@ssd.csd.harris.com | Fort Lauderdale, FL 33309 USA | | ... {uunet | novavax}!hcx1!brad | MailStop 161 (305) 973-5007 | +=-=-=-=-=-=-=-=- DISCLAIMER: I said it, not my company! -=-=-=-=-=-=-=-=-=-+