-
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.
-
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.