home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3477 < prev    next >
Encoding:
Text File  |  1992-08-13  |  1.6 KB  |  53 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!scorn!scolex!charless
  3. From: charless@sco.COM (charles stross)
  4. Subject: Re: Echoing Shell Variables
  5. Organization: The Somewhat Contagious Operation, Inc.
  6. Date: Wed, 12 Aug 1992 10:46:38 GMT
  7. Message-ID: <1992Aug12.104638.24076@sco.COM>
  8. References: <2647@nlsun1.oracle.nl>
  9. Sender: news@sco.COM (Account for Usenet System)
  10. Lines: 41
  11.  
  12.  
  13. In article <2647@nlsun1.oracle.nl> rgasch@nl.oracle.com (Robert Gasch) writes:
  14. >
  15. >I have a little problem that's quite annoying. I have written
  16. >a shell script which receives as it's argument than name of
  17. >a shell variable (ex: PATH) via $1.
  18. >
  19. >The big question is: How can I echo the contents of the variable
  20. >whose name I get? For example: If $1 contains the string PATH,
  21. >I want to be able to do the equivalent of saying "echo $PATH" at
  22. >the command prompt so that I receive a list of directories 
  23. >which are in the path.
  24. >For some reason I can't figure this one out. The only thing
  25. >I've been able to get is PATH (the contents of the argument)
  26. >and errors.
  27. >Any help on this will be much appreciated
  28. >---> Rob
  29. >
  30.  
  31. Try this:
  32.  
  33. #!/bin/ksh
  34. eval echo \$$1
  35.  
  36. (It works for me :)
  37.  
  38. Attempted explanation:
  39.  
  40. The eval command reads and executes the Korn shell command line that 
  41. follows it. You use it to induce the shell to scan the command line 
  42. twice before executing it. On the first pass, the shell replaces \$ 
  43. with a literal $ and expands $1 (say, PATH). On the second pass it 
  44. sees the command echo $PATH.
  45.  
  46. -- 
  47. Charlie Stross   aka  charless@scol.sco.com
  48. UNIX-oriented text mangler
  49. This 'ere is my opinion;
  50. now't t'do wi' SCO.
  51.