eval command

This is a discussion on eval command within the Linux Programming forums, part of the Platform Specific Boards category; Hi guys, I understand that eval will evaluate an expression and try to execute the returning command. like Code: x=ls ...

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    eval command

    Hi guys,

    I understand that eval will evaluate an expression and try to execute the returning command.
    like
    Code:
    x=ls
    eval $x
    #will display files/dir
    but if i use the following code
    Code:
    x=ls
    $x
    #will display files/dir
    still ls will get executed. so where lies the advantage of using eval? What are other situtations eval can be used in?

    Thanks a lot for your replies in advance.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    I'm finding it hard to put into words, but consider this example:

    Code:
    VAR1=ls
    VAR2=VAR1
    eval \$$VAR2
    In other words, VAR2 contains the name of another variable and I want to extract the value from the variable it references. It's almost like VAR2 is a pointer which points to VAR1, and the "eval" command is how you dereference it.

    Try to accomplish what that example does without using eval. I think you'll find that you can't do it. That's why eval exists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Ping problem
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 02-02-2005, 11:54 AM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21