home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10102 < prev    next >
Encoding:
Text File  |  1992-08-14  |  1.8 KB  |  75 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!usc!rpi!fitzgb
  3. From: fitzgb@mml0.meche.rpi.edu (Brian Fitzgerald)
  4. Subject: Re: why use eval?
  5. Message-ID: <=zaylbf@rpi.edu>
  6. Summary: eval is your pal
  7. Nntp-Posting-Host: mml0.meche.rpi.edu
  8. Organization: Rensselaer Polytechnic Institute, Troy, NY
  9. References: <92Aug14.195242.10973@acs.ucalgary.ca> <92Aug14.215513.11933@acs.ucalgary.ca>
  10. Date: Sat, 15 Aug 1992 05:00:52 GMT
  11. Lines: 62
  12.  
  13. I W Scott Barker writes:
  14. >> What is the significance of using eval `[command]`? The man pages state that
  15. >> this runs the command [command] in the shell. Does not simply typing the
  16. >> command alone accomplish the same thing?
  17. >
  18. >I believe I have figured out the use of eval. The back-quotes around a command
  19. >cause that command to be executed in a sub-shell, after command/filename/
  20. >variable substitution, and the resulting output is passed back to the shell as
  21. >text. No further substitution is performed on that text. If, however, the
  22. >back-quoted command is an argument to eval, substition IS performed on the
  23. >resulting text. Thus, for example:
  24. >
  25. >alias ls 'ls -Fa'
  26. >set foo=ls
  27. >`echo $foo`
  28. >
  29. >results in a "simple" ls being performed, while
  30. >
  31. >alias ls 'ls -Fa'
  32. >set foo=ls
  33. >eval `echo $foo`
  34. >
  35. >results in an ls -Fa being performed, since eval caused further command
  36. >substition to be performed.
  37. >
  38. >Is this correct?
  39.  
  40. I think so
  41.  
  42. Another application of eval is the expansion of variables which contain
  43. metacharacters intended for interpretation interpreted by the current
  44. shell.
  45.  
  46. For example (sh):
  47.  
  48. pipe="deroff -w | uniq -d"
  49. $pipe
  50.  
  51. fails with
  52. Deroff: |: No such file or directory
  53.  
  54. But:
  55.  
  56. eval $pipe
  57.  
  58. works fine.
  59.  
  60. As another example, the presence of the ";" in 
  61.  
  62. export TERMCAP TERM;
  63. TERM=vt100;
  64. TERMCAP='d0|vt100 ...
  65.  
  66. (the output of "tset -s") requires that tset be invoked as
  67.  
  68. eval `tset -s ... `
  69.  
  70. and not merely
  71.  
  72. `tset -s ... `
  73.  
  74. Brian
  75.