home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!usc!rpi!fitzgb
- From: fitzgb@mml0.meche.rpi.edu (Brian Fitzgerald)
- Subject: Re: why use eval?
- Message-ID: <=zaylbf@rpi.edu>
- Summary: eval is your pal
- Nntp-Posting-Host: mml0.meche.rpi.edu
- Organization: Rensselaer Polytechnic Institute, Troy, NY
- References: <92Aug14.195242.10973@acs.ucalgary.ca> <92Aug14.215513.11933@acs.ucalgary.ca>
- Date: Sat, 15 Aug 1992 05:00:52 GMT
- Lines: 62
-
- I W Scott Barker writes:
- >> What is the significance of using eval `[command]`? The man pages state that
- >> this runs the command [command] in the shell. Does not simply typing the
- >> command alone accomplish the same thing?
- >
- >I believe I have figured out the use of eval. The back-quotes around a command
- >cause that command to be executed in a sub-shell, after command/filename/
- >variable substitution, and the resulting output is passed back to the shell as
- >text. No further substitution is performed on that text. If, however, the
- >back-quoted command is an argument to eval, substition IS performed on the
- >resulting text. Thus, for example:
- >
- >alias ls 'ls -Fa'
- >set foo=ls
- >`echo $foo`
- >
- >results in a "simple" ls being performed, while
- >
- >alias ls 'ls -Fa'
- >set foo=ls
- >eval `echo $foo`
- >
- >results in an ls -Fa being performed, since eval caused further command
- >substition to be performed.
- >
- >Is this correct?
-
- I think so
-
- Another application of eval is the expansion of variables which contain
- metacharacters intended for interpretation interpreted by the current
- shell.
-
- For example (sh):
-
- pipe="deroff -w | uniq -d"
- $pipe
-
- fails with
- Deroff: |: No such file or directory
-
- But:
-
- eval $pipe
-
- works fine.
-
- As another example, the presence of the ";" in
-
- export TERMCAP TERM;
- TERM=vt100;
- TERMCAP='d0|vt100 ...
-
- (the output of "tset -s") requires that tset be invoked as
-
- eval `tset -s ... `
-
- and not merely
-
- `tset -s ... `
-
- Brian
-