Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.unix.questions Subject: Re: unix shell programming question Summary: Use eval Message-ID: <1990Feb26.145323.22611@virtech.uucp> Date: 26 Feb 90 14:53:23 GMT References: <504@lkbpyr.UUCP> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 34 In article <504@lkbpyr.UUCP> jonas@lkbpyr.UUCP (Jonas Heyman) writes: >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 Replace the indicated lines with: eval aa=\$str$loop #Here I want "string 1" and "string 2" and eval echo \$str$loop #Here I want "string 1" and "string 2" respectively, and you will get the output you desire. Eval just tells the shell to read the arguments as input and execute them. this causes the rest of the line to be scanned twice. On the first scan, the \$str is converted to $str and the $loop is converted to 1 or 2. The second scan interprets the $str1 or $str2 -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+